2024-01-16 01:08:21 +03:00
|
|
|
package gg
|
|
|
|
|
|
|
|
type Edges []Edge
|
|
|
|
// Get crosses of edges.
|
2024-01-22 10:23:47 +03:00
|
|
|
func (what Edges) CrossWithEdges(with Edges) ([]LineCross, bool) {
|
2024-01-16 01:08:21 +03:00
|
|
|
ret := []LineCross{}
|
|
|
|
for i := range what {
|
|
|
|
for j := range with {
|
|
|
|
cross, doCross := LinersCross(what[i], with[j])
|
|
|
|
if doCross {
|
|
|
|
ret = append(ret, cross)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 10:23:47 +03:00
|
|
|
return ret, len(ret) > 0
|
2024-01-16 01:08:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The type represents a line segment. The name is for short.
|
|
|
|
type Edge [2]Vector
|
|
|
|
|
|
|
|
// Returns corresponding to the segment Line.
|
|
|
|
func (l Edge) Line() Line {
|
2024-01-22 10:23:47 +03:00
|
|
|
var (
|
|
|
|
x Float
|
|
|
|
vertical bool
|
|
|
|
)
|
|
|
|
|
2024-01-16 01:08:21 +03:00
|
|
|
p0 := l[0]
|
|
|
|
p1 := l[1]
|
|
|
|
|
|
|
|
k := (p0.Y - p1.Y) / (p0.X - p1.X)
|
|
|
|
c := p0.Y - p0.X*k
|
2024-01-22 10:23:47 +03:00
|
|
|
if p0.X == p1.X {
|
|
|
|
x = p0.X
|
2024-01-22 20:08:50 +03:00
|
|
|
vertical = true
|
2024-01-22 10:23:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Line{k, c, x, vertical}
|
2024-01-16 01:08:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l Edge) ContainedPoints(pts Points) (Points) {
|
|
|
|
ret := Points{}
|
|
|
|
for i := range pts {
|
|
|
|
if l.ContainsPoint(pts[i]) {
|
|
|
|
ret = append(ret, pts[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the edge contains the specified point.
|
|
|
|
func (l Edge) ContainsPoint(p Point) bool {
|
|
|
|
line := l.Line()
|
|
|
|
if !line.ContainsPoint(p) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-01-22 10:23:47 +03:00
|
|
|
xMax := Max(l[0].X, l[1].X)
|
|
|
|
xMin := Min(l[0].X, l[1].X)
|
2024-01-16 01:08:21 +03:00
|
|
|
|
2024-01-22 10:23:47 +03:00
|
|
|
yMax := Max(l[0].Y, l[1].Y)
|
|
|
|
yMin := Min(l[0].Y, l[1].Y)
|
2024-01-16 01:08:21 +03:00
|
|
|
|
2024-01-22 20:08:50 +03:00
|
|
|
if !(xMin <= p.X && p.X <= xMax) ||
|
|
|
|
!(yMin <= p.Y && p.Y <= yMax) {
|
2024-01-16 01:08:21 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get square of length of line segment (for performance sometimes).
|
|
|
|
func (ls Edge) LenSqr() Float {
|
|
|
|
return Sqr(ls[0].X - ls[1].X) +
|
|
|
|
Sqr(ls[0].Y - ls[1].Y)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get length of the line segment.
|
|
|
|
func (ls Edge) Len() Float {
|
|
|
|
return Sqrt(ls.LenSqr())
|
|
|
|
}
|
|
|
|
|