Example #1
0
// Generic version of Draw
func Draw(pm nimble.PixMap, harmonics []Harmonic, cm colorMap) {
	setColoring(cm)
	n := len(harmonics)
	w := make([]complex64, n)
	u := make([]complex64, n)
	v := make([]complex64, n)
	z := make([]complex64, n)
	for i, h := range harmonics {
		w[i] = complex(h.Amplitude*clutRadius, 0) * euler(h.Phase)
		u[i] = euler(h.Ωx)
		v[i] = euler(h.Ωy)
	}
	for y := int32(0); y < pm.Height(); y++ {
		for i := 0; i < n; i++ {
			z[i] = w[i]
			w[i] *= v[i] // Rotate w by v
		}
		row := pm.Row(y)
		for x := range row {
			const offset float32 = clutCenter + 0.5
			s := complex(offset, offset)
			for i := 0; i < n; i++ {
				s += z[i]
				z[i] *= u[i]
			}
			row[x] = clut[int(imag(s))][int(real(s))]
		}
	}
}
Example #2
0
// Draw draws the given sprite at (x0,y0) on dst with the given color.
func Draw(dst nimble.PixMap, x0, y0 int32, src Sprite, color nimble.Pixel) {
	w, h := dst.Size()
	for _, s := range src.rows {
		y := y0 + int32(s.y)
		if uint32(y) < uint32(h) {
			d := dst.Row(y)
			for _, xoffset := range s.x {
				x := x0 + int32(xoffset)
				if uint32(x) < uint32(w) {
					d[x] = color
				}
			}
		}
	}
}
Example #3
0
// Draw draws a Fourier transform on the given PixMap.
// Transform values must lie on the unit circle in the complex plane.
func Draw(pm nimble.PixMap, harmonics []Harmonic, cm colorMap) {
	setColoring(cm)
	n := len(harmonics)
	// m = n rounded up to even
	m := n + n&1
	u := uStorage[:m]
	v := vStorage[:m]
	w := wStorage[:m]
	for i, h := range harmonics {
		for k := 0; k < vlen; k++ {
			d := h.Amplitude * clutRadius
			c := euler(h.Phase + float32(k+vlen)*h.Ωx)
			w[i].re[k] = d * real(c)
			w[i].im[k] = d * imag(c)
		}
		u[i].u1 = euler(vlen * h.Ωx)
		u[i].u3 = euler(pixelsPerFoot * h.Ωx)
		v[i] = euler(h.Ωy)
	}
	if n < m {
		// Zero the extra element.
		w[n] = cvec{}
		u[n] = u13{}
		v[n] = 0
	}
	width, height := pm.Size()
	p := width / pixelsPerFoot // Number of whole feet
	q := p * pixelsPerFoot     // Number of pixels in whole feet
	r := width - q             // Number of pixels in partial foot
	feet := feetStorage[:(width+pixelsPerFoot-1)/pixelsPerFoot]
	for y := int32(0); y < height; y++ {
		for i := 0; i < n; i += 2 {
			accumulateToFeet(
				(*[2]cvec)(unsafe.Pointer(&w[i])),
				(*[2]u13)(unsafe.Pointer(&u[i])), feet)
		}
		rotate(w, v)
		feetToPixel(feet[:p], &clut, pm.Row(y))
		if r != 0 {
			feetToPixel(feet[p:p+1], &clut, tmpStorage[:])
			copy(pm.Row(y)[q:q+r], tmpStorage[:r])
		}
	}
}
Example #4
0
func draw(pm nimble.PixMap, text [][]byte) {
	if teletypeFont == nil {
		panic("teletype font missing")
	}
	width, height := pm.Size()

	// Clear area
	pm.Fill(nimble.Black)

	// Write lines of text
	for m := range text {
		x := int32(textLeftMargin)
		for j := range text[m] {
			if x >= width {
				break
			}
			kLimit := width - x
			if kLimit > charWidth {
				kLimit = charWidth
			}
			c := text[m][j]
			for i, mask := range teletypeFont[c] {
				y := int32(textTopMargin + m*textLineHeight + i)
				if y >= height {
					break
				}
				pixelRow := pm.Row(y)[x : x+kLimit]
				colorIndex := 0
				for k := range pixelRow {
					if mask&(1<<uint(k)) != 0 {
						pixelRow[k] = teletypeColor[colorIndex]
						colorIndex++
					} else {
						colorIndex = 0
					}
				}
			}
			x += charWidth
		}
	}
}