Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polygon.isSelfIntersecting doesn't work at all #16

Closed
sroik opened this issue Feb 15, 2019 · 2 comments
Closed

Polygon.isSelfIntersecting doesn't work at all #16

sroik opened this issue Feb 15, 2019 · 2 comments

Comments

@sroik
Copy link

sroik commented Feb 15, 2019

U need to consider edge points

screen shot 2019-02-15 at 3 02 30 pm

@iby
Copy link
Collaborator

iby commented Feb 22, 2019

@toineheuvelmans This is caused by point reordering in the init:

/// The default initializer, returns a `Polygon` given
/// the provided array of `CGPoints`. Note that the points
/// will be rearranged in such a way that certain
/// calculations can be done more easily.
/// The initializer asserts on being given at least 3 points.
public init(points: [CGPoint]) {
assert(points.count > 2, "A polygon should at least have 3 points.")
// Store them in "clockwise" direction (sum of edges should be positive)
var lastPoint = points.last!
var edgeSum: CGFloat = 0.0
for point in points {
let edge = (lastPoint.x - point.x) * (lastPoint.y + point.y)
edgeSum += edge
lastPoint = point
}
let clockwisePoints = (edgeSum >= 0) ? points : points.reversed()
// Start at the minXminY point
let minXminYPoint = clockwisePoints.reduce(clockwisePoints[0], { (($1.x <= $0.x) && ($1.y <= $0.y)) ? $1 : $0 })
let index = clockwisePoints.index(of: minXminYPoint)!
let pointsFromIndex = index == 0 ? clockwisePoints : Array((clockwisePoints[index..<clockwisePoints.count] + clockwisePoints[0..<index]))
self.points = pointsFromIndex
}

I'm guessing …certain calculations can be done more easily… is about convex hull? Do you remember if there is anything else that relies on this?

@toineheuvelmans
Copy link
Owner

@sroik It ensures the points and thus line segments are in order. If I recall correctly this is indeed for calculating the convex hull but also for anything that iterates over the points / line segments, so area, lineSegments, isConvex and path.

@sroik sroik closed this as completed Aug 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants