7 "git.friedersdorff.com/max/vector.go"
12 // sameSide returns true if p1 and p2 are on the same side of the line ab, false
14 func sameSide(p1, p2, a, b vector.Vec) bool {
15 cp1 := b.Sub(a).Cross(p1.Sub(a))
16 cp2 := b.Sub(a).Cross(p2.Sub(a))
18 return cp1.Dot(cp2) >= 0
21 // inTriangle returns true if point p is in triangle abc, false otherwise
22 func inTriangle(p, a, b, c vector.Vec) bool {
23 return sameSide(p, a, b, c) && sameSide(p, b, a, c) && sameSide(p, c, a, b)
26 func getVec(record []string) ([]vector.Vec, error) {
27 if len(record)%2 != 0 {
28 return nil, errors.New(
29 fmt.Sprintf("Length of record is not divisible by 2: %s",
34 floats := make([]float64, len(record))
35 for i, v := range record {
36 num, err := strconv.ParseFloat(v, 64)
39 return nil, errors.New(
40 fmt.Sprintf("Couldn't parse record to floats: %s",
47 for i := 0; i < len(floats); i += 2 {
48 vecs = append(vecs, vector.Vec{floats[i], floats[i+1], 0.0})
55 if len(os.Args) != 2 {
56 fmt.Println("Expect input file as first argument.")
60 f, err := os.Open(os.Args[1])
71 triangle, err := r.Read()
76 points, err := getVec(triangle)
86 if inTriangle(vector.Vec{0, 0, 0},
87 points[0], points[1], points[2]) {
92 fmt.Println(nInTriangle)