1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| const ( _TRIANGLE_SIDE = false )
func triangle_cross(a, b, c image.Point) int { ab := image.Point{b.X - a.X, b.Y - a.Y} ac := image.Point{c.X - a.X, c.Y - a.Y} return ab.X*ac.Y - ab.Y*ac.X }
func triangle_right_side(a, b, c image.Point) bool { if triangle_cross(a, b, c) > 0 { return true } return false }
func triangle_sort(a, b, c image.Point) (image.Point, image.Point, image.Point) { sort := []image.Point{a, b, c} n := 0 for i := 1; i < 3; i++ { if sort[i].Y < sort[n].Y { n = i } else if sort[i].Y == sort[n].Y { if sort[i].X < sort[n].X { n = i } } } if n != 0 { t := sort[0] sort[0] = sort[n] sort[n] = t } if triangle_right_side(sort[0], sort[1], sort[2]) == _TRIANGLE_SIDE { return sort[0], sort[1], sort[2] } return sort[0], sort[2], sort[1] }
func triangle_min(a, b int) int { if a < b { return a } return b }
func triangle_max(a, b int) int { if a > b { return a } return b }
func triangle_1(img *image.RGBA, A, B, C image.Point, c color.Color) { A, B, C = triangle_sort(A, B, C)
x_min, x_max := A.X, A.X y_min, y_max := A.Y, A.Y
x_min = triangle_min(x_min, B.X) x_min = triangle_min(x_min, C.X) x_max = triangle_max(x_max, B.X) x_max = triangle_max(x_max, C.X)
y_min = triangle_min(y_min, B.Y) y_min = triangle_min(y_min, C.Y) y_max = triangle_max(y_max, B.Y) y_max = triangle_max(y_max, C.Y)
for i := x_min; i <= x_max; i++ { for j := y_min; j <= y_max; j++ { D := image.Point{i, j} if triangle_right_side(A, B, D) == _TRIANGLE_SIDE && triangle_right_side(B, C, D) == _TRIANGLE_SIDE && triangle_right_side(C, A, D) == _TRIANGLE_SIDE { img.Set(i, j, c) } } } }
|