convex test

This commit is contained in:
zzzrrr 2009-07-15 19:03:45 -04:00
parent 735fe986b9
commit 2a27491146
3 changed files with 20 additions and 16 deletions

View File

@ -44,6 +44,8 @@ class MonotoneMountain {
// Triangles that constitute the mountain // Triangles that constitute the mountain
val triangles = new ArrayBuffer[Array[Point]] val triangles = new ArrayBuffer[Array[Point]]
var slop = 0.0f
// Append a point to the list // Append a point to the list
def +=(point: Point) { def +=(point: Point) {
size match { size match {
@ -92,7 +94,7 @@ class MonotoneMountain {
val ear = convexPoints.dequeue val ear = convexPoints.dequeue
val a = ear.prev.clone val a = ear.prev.clone
val b = ear val b = ear.clone
val c = ear.next.clone val c = ear.next.clone
val triangle = Array(a, b, c) val triangle = Array(a, b, c)
triangles += triangle triangles += triangle
@ -103,6 +105,11 @@ class MonotoneMountain {
if(c.prev != null && convex(c)) convexPoints.enqueue(c) 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") assert(size <= 3, "Triangulation bug")
if(size == 3)lastTriangle 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 // Determines if the inslide angle between edge v2-v3 and edge v2-v1 is convex
private def convex(p: Point) = { private def convex(p: Point) = {
val a = (p.next - p) val a = (p.next - p).normalize
val b = (p.prev - p) val b = (p.prev - p).normalize
var angle = Math.atan2(b.y,b.x) - Math.atan2(a.y,a.x) slop = a dot b
if(angle < 0) while(angle < -Math.Pi) angle += Math.Pi slop = 0.1f * Math.round( slop * 10.0f)
if(angle > 0) while(angle > Math.Pi) angle -= Math.Pi (slop >= 0f || slop == -1f || slop == 1f)
// For numerical robustness....
angle = 0.1 * Math.round( angle * 10.0)
val cvx = (angle < 0)
if(p.y >= head.y) {
cvx
} else {
!cvx
}
} }
private def lastTriangle { private def lastTriangle {

View File

@ -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 +(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 *(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) = { def <(p: Point) = {
if(p.x == x) if(p.x == x)

View File

@ -85,8 +85,8 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
} }
val lCirc = new Circle(t.leftPoint.x, t.leftPoint.y, 4) val lCirc = new Circle(t.leftPoint.x, t.leftPoint.y, 4)
g.setColor(blue); g.draw(lCirc); g.fill(lCirc) g.setColor(blue); g.draw(lCirc); g.fill(lCirc)
val rCirc = new Circle(t.rightPoint.x, t.rightPoint.y, 6) val rCirc = new Circle(t.rightPoint.x, t.rightPoint.y, 4)
//g.setColor(yellow); g.draw(rCirc); g.fill(rCirc) g.setColor(yellow); g.draw(rCirc); g.fill(rCirc)
g.setColor(red) g.setColor(red)
g.draw(polygon) g.draw(polygon)
} }
@ -95,6 +95,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
if(!debug) { if(!debug) {
var i = 0 var i = 0
for(t <- tesselator.triangles) { for(t <- tesselator.triangles) {
if(t.size < 3) println("wtf")
val triangle = new Polygon val triangle = new Polygon
t.foreach(p => triangle.addPoint(p.x, p.y)) t.foreach(p => triangle.addPoint(p.x, p.y))
val color = if(i == hiLighter) blue else red val color = if(i == hiLighter) blue else red