From 2a274911462151966dac3f9633cc304d5ae18d07 Mon Sep 17 00:00:00 2001 From: zzzrrr Date: Wed, 15 Jul 2009 19:03:45 -0400 Subject: [PATCH] convex test --- src/org/poly2tri/MonoToneMountain.scala | 27 ++++++++++++------------- src/org/poly2tri/Point.scala | 4 ++++ src/org/poly2tri/Poly2Tri.scala | 5 +++-- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/org/poly2tri/MonoToneMountain.scala b/src/org/poly2tri/MonoToneMountain.scala index e6176ce..eec657e 100644 --- a/src/org/poly2tri/MonoToneMountain.scala +++ b/src/org/poly2tri/MonoToneMountain.scala @@ -44,6 +44,8 @@ class MonotoneMountain { // Triangles that constitute the mountain val triangles = new ArrayBuffer[Array[Point]] + var slop = 0.0f + // Append a point to the list def +=(point: Point) { size match { @@ -92,7 +94,7 @@ class MonotoneMountain { val ear = convexPoints.dequeue val a = ear.prev.clone - val b = ear + val b = ear.clone val c = ear.next.clone val triangle = Array(a, b, c) triangles += triangle @@ -103,6 +105,11 @@ class MonotoneMountain { if(c.prev != null && convex(c)) convexPoints.enqueue(c) } + if(size >= 4) { + println("Size = " + size) + println(convex(head.next) + ", Angle: " + slop) + println(convex(head.next.next) + ", Angle: " + slop) + } assert(size <= 3, "Triangulation bug") if(size == 3)lastTriangle } @@ -119,19 +126,11 @@ class MonotoneMountain { // Determines if the inslide angle between edge v2-v3 and edge v2-v1 is convex private def convex(p: Point) = { - val a = (p.next - p) - val b = (p.prev - p) - var angle = Math.atan2(b.y,b.x) - Math.atan2(a.y,a.x) - if(angle < 0) while(angle < -Math.Pi) angle += Math.Pi - if(angle > 0) while(angle > Math.Pi) angle -= Math.Pi - // For numerical robustness.... - angle = 0.1 * Math.round( angle * 10.0) - val cvx = (angle < 0) - if(p.y >= head.y) { - cvx - } else { - !cvx - } + val a = (p.next - p).normalize + val b = (p.prev - p).normalize + slop = a dot b + slop = 0.1f * Math.round( slop * 10.0f) + (slop >= 0f || slop == -1f || slop == 1f) } private def lastTriangle { diff --git a/src/org/poly2tri/Point.scala b/src/org/poly2tri/Point.scala index e402371..6e32b5c 100644 --- a/src/org/poly2tri/Point.scala +++ b/src/org/poly2tri/Point.scala @@ -44,6 +44,10 @@ class Point(val x: Float, val y: Float, var segment: Segment) { def +(p: Point) = new Point(x + p.x, y + p.y) def +(f: Float) = new Point(x + f, y + f) def *(f: Float) = new Point(x * f, y * f) + def /(a: Float) = new Point(x / a, y / a) + def dot(p: Point) = x * p.x + y * p.y + def length = Math.sqrt(x * x + y * y).toFloat + def normalize = this / length def <(p: Point) = { if(p.x == x) diff --git a/src/org/poly2tri/Poly2Tri.scala b/src/org/poly2tri/Poly2Tri.scala index 3daaba3..c5a6d71 100644 --- a/src/org/poly2tri/Poly2Tri.scala +++ b/src/org/poly2tri/Poly2Tri.scala @@ -85,8 +85,8 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") { } val lCirc = new Circle(t.leftPoint.x, t.leftPoint.y, 4) g.setColor(blue); g.draw(lCirc); g.fill(lCirc) - val rCirc = new Circle(t.rightPoint.x, t.rightPoint.y, 6) - //g.setColor(yellow); g.draw(rCirc); g.fill(rCirc) + val rCirc = new Circle(t.rightPoint.x, t.rightPoint.y, 4) + g.setColor(yellow); g.draw(rCirc); g.fill(rCirc) g.setColor(red) g.draw(polygon) } @@ -95,6 +95,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") { if(!debug) { var i = 0 for(t <- tesselator.triangles) { + if(t.size < 3) println("wtf") val triangle = new Polygon t.foreach(p => triangle.addPoint(p.x, p.y)) val color = if(i == hiLighter) blue else red