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
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 {

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 +(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)

View File

@ -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