From 251e8ed8ff0eb7ecb9c09270d266c63116bfc897 Mon Sep 17 00:00:00 2001 From: zzzzrrr Date: Tue, 18 Aug 2009 10:22:16 -0400 Subject: [PATCH] changed edge initialization --- src/org/poly2tri/cdt/CDT.scala | 34 +++++++++++++++++++++++---- src/org/poly2tri/shapes/Segment.scala | 23 +----------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/org/poly2tri/cdt/CDT.scala b/src/org/poly2tri/cdt/CDT.scala index f5076b4..56ddd3f 100644 --- a/src/org/poly2tri/cdt/CDT.scala +++ b/src/org/poly2tri/cdt/CDT.scala @@ -81,16 +81,41 @@ object CDT { // Create segments and connect end points; update edge event pointer private def initSegments(points: ArrayBuffer[Point]): List[Segment] = { + var segments = List[Segment]() for(i <- 0 until points.size-1) { - segments = new Segment(points(i), points(i+1)) :: segments - segments.first.updateEdge + + val endPoints = validatePoints(points(i), points(i+1)) + segments = new Segment(endPoints(0), endPoints(1)) :: segments + endPoints(1).edges += segments.first + } - segments = new Segment(points.first, points.last) :: segments - segments.first.updateEdge + + val endPoints = validatePoints(points.first, points.last) + segments = new Segment(endPoints(0), endPoints(1)) :: segments + endPoints(1).edges += segments.first + segments } + def validatePoints(p1: Point, p2: Point): List[Point] = { + + if(p1.y > p2.y) { + // For CDT we want q to be the point with > y + return List(p2, p1) + } else if(p1.y == p2.y) { + // If y values are equal, make sure point with smaller x value + // is the the left + if(p1.x > p2.x) { + return List(p2, p1) + } else if(p1.x == p2.x) { + throw new Exception("Duplicate point") + } + } + + List(p1, p2) + } + // Insertion sort is one of the fastest algorithms for sorting arrays containing // fewer than ten elements, or for lists that are already mostly sorted. // Merge sort: O(n log n) @@ -259,6 +284,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val first = pNode val points = new ArrayBuffer[Point] + // Neighbor triangles val nTriangles = new ArrayBuffer[Triangle] nTriangles += pNode.triangle diff --git a/src/org/poly2tri/shapes/Segment.scala b/src/org/poly2tri/shapes/Segment.scala index 62fbe4e..9487e29 100644 --- a/src/org/poly2tri/shapes/Segment.scala +++ b/src/org/poly2tri/shapes/Segment.scala @@ -50,26 +50,5 @@ class Segment(var p: Point, var q: Point) { def > (point: Point) = (Math.floor(point.y) < Math.floor(slope * point.x + b)) // Determines if this segment lies below the given point def < (point: Point) = (Math.floor(point.y) > Math.floor(slope * point.x + b)) - - // Update point edge list for CDT - def updateEdge { - if(p.y > q.y) { - // For CDT we want q to be the point with > y - val tmp = p - p = q - q = tmp - } else if(p.y == q.y) { - // If y values are equal, make sure point with smaller x value - // is the the left - if(p.x > q.x) { - val tmp = p - p = q - q = tmp - } else if(p.x == q.x) { - throw new Exception("Duplicate point") - } - } - q.edges += this - } - + }