Example #1
0
// Draw the square.
// Remember that the square should be ordered previously to make sure it
// prints in the intended order. Take a look at
// `github.com/Willyfrog/peano/matrix.Strategy` Interface
func (sq *Square) Draw(canvas *drawing.Canvas) {
	path := canvas.GetContext()
	if log.StandardLogger().Level == log.DebugLevel {
		if sq.X == 0 && sq.Y == 0 {
			path.SetStrokeColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
		} else if sq.X == 0 && sq.Y == 1 {
			path.SetStrokeColor(color.RGBA{0xff, 0x44, 0x44, 0xff})
		} else {
			path.SetStrokeColor(color.RGBA{0xcc, 0xcc, 0xcc, 0xff})
		}
	} else {
		path.SetStrokeColor(color.RGBA{0xcc, 0xcc, 0xcc, 0xff})
	}

	path.SetLineWidth(1)
	xo, yo := sq.Origin()
	xe, ye := sq.End()
	drawing.DrawSquare(xo, yo, xe, ye, path)
	path.Stroke()
	//path.FillStroke()
	//log.Debug(fmt.Sprintf("Result: %v", sq.Points))
	var origin *point.Point
	for _, pt := range sq.Points {
		pt.Draw(canvas)
		if origin != nil {
			linepath := canvas.GetContext()
			path.SetStrokeColor(color.RGBA{0x44, 0x44, 0x88, 0xff})
			path.SetLineWidth(5)
			drawing.DrawLine(origin.X, origin.Y, pt.X, pt.Y, linepath)
			linepath.Stroke()
		}
		origin = pt
	}
}
Example #2
0
// Draw the matrix into the canvas using the `strat` Strategy
func (m *Matrix) Draw(canvas *drawing.Canvas, strat Strategy) {
	path := canvas.GetContext()
	path.SetFillColor(color.RGBA{0xaa, 0xaa, 0xaa, 0xff})
	path.SetStrokeColor(color.RGBA{0x99, 0x99, 0x99, 0xff})
	path.SetLineWidth(2)
	drawing.DrawSquare(0.0, 0.0, 1.0, 1.0, path)
	log.Info("filling the background of the image, this might take some seconds")
	path.FillStroke()
	m.drawSquares(canvas, strat)
}
Example #3
0
func (m *Matrix) drawSquares(canvas *drawing.Canvas, strat Strategy) {
	finished := make(chan position)
	numSquares := len(m.Squares) * len(m.Squares)
	sentSquares := 0
	for i, row := range m.Squares {
		for j := range row {
			go fillSquare(m.Squares[i][j], finished, strat)
			sentSquares++
			log.Debug(fmt.Sprintf("Sent [%d, %d] %d/%d", i, j, sentSquares, numSquares))
		}
	}
	log.Debug("Waiting for squares to be filled.")
	wait := make(chan bool)
	go drawEach(finished, wait, numSquares, *m, canvas)
	_ = <-wait //synchronize
	path := canvas.GetContext()
	//path.SetFillColor(color.RGBA{0xaa, 0xaa, 0xaa, 0xff})
	path.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
	path.SetLineWidth(5)
	drawing.DrawSquare(0.0, 0.0, 1.0, 1.0, path)
	//path.FillStroke()
	log.Debug("About to draw line connections")
	for i, line := range strat.ConnectSquares(*m) {
		//log.Debug(fmt.Sprintf("Drawing the %dth line: %v", i, line))
		if len(line) == 2 {
			p1 := line[0]
			p2 := line[1]
			path2 := canvas.GetContext()
			//path2.SetFillColor(color.RGBA{0xaa, 0xaa, 0xaa, 0xff})
			path2.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
			path2.SetLineWidth(1)
			drawing.DrawLine(p1.X, p1.Y, p2.X, p2.Y, path2)
			path2.FillStroke()
		} else {
			log.Error(fmt.Sprintf("Line %d didn't contain 2 points", i)) // shouldn't happen :/
		}
	}
}