added snake and debug controls

This commit is contained in:
zzzrrr 2009-07-14 13:17:56 -04:00
parent d0e1720634
commit 2dcfa82af2
4 changed files with 64 additions and 25 deletions

View File

@ -99,7 +99,7 @@ class MonotoneMountain {
p.angle = Math.abs(angle(p.prev, p, p.next)) p.angle = Math.abs(angle(p.prev, p, p.next))
println("angle = " + p.angle) println("angle = " + p.angle)
// Link strictly convex vertices into a list // Link strictly convex vertices into a list
if(p.angle > 0 && p.angle <= Math.Pi) convexPoints.enqueue(p) if(p.angle >= 0 && p.angle <= Math.Pi) convexPoints.enqueue(p)
p = p.next p = p.next
} }
@ -114,12 +114,12 @@ class MonotoneMountain {
// Remove ear, update angles and convex list // Remove ear, update angles and convex list
remove(ear) remove(ear)
a.angle -= ear.angle a.angle -= ear.angle
if(a.angle > 0) convexPoints.enqueue(a) if(a.angle > 0 && a != head && a != tail) convexPoints.enqueue(a)
c.angle -= ear.angle c.angle -= ear.angle
if(c.angle > 0) convexPoints.enqueue(c) if(c.angle > 0 && c != head && c != tail) convexPoints.enqueue(c)
} }
if(size == 3) lastTriangle if(size > 2)lastTriangle
} }
} }

View File

@ -36,7 +36,7 @@ package org.poly2tri
// "Computational Geometry in C", 2nd edition, by Joseph O'Rourke // "Computational Geometry in C", 2nd edition, by Joseph O'Rourke
import org.newdawn.slick.{BasicGame, GameContainer, Graphics, Color, AppGameContainer} import org.newdawn.slick.{BasicGame, GameContainer, Graphics, Color, AppGameContainer}
import org.newdawn.slick.geom.Polygon import org.newdawn.slick.geom.{Polygon, Circle}
import collection.jcl.ArrayList import collection.jcl.ArrayList
@ -55,10 +55,14 @@ object Poly2Tri {
class Poly2TriDemo extends BasicGame("Poly2Tri") { class Poly2TriDemo extends BasicGame("Poly2Tri") {
var tesselator: Triangulator = null var tesselator: Triangulator = null
var quit = false var quit = false
var debug = false
var drawMap = false
def init(container: GameContainer) { def init(container: GameContainer) {
testTesselator testTesselator
//snake
} }
def update(gc: GameContainer, delta: Int) { def update(gc: GameContainer, delta: Int) {
@ -67,19 +71,26 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
def render(container: GameContainer, g: Graphics) { def render(container: GameContainer, g: Graphics) {
val red = new Color(1f,0.0f,0.0f) val red = new Color(1f, 0f,0.0f)
val blue = new Color(0f, 0f, 1f) val blue = new Color(0f, 0f, 1f)
val green = new Color(0f, 1f, 0f) val green = new Color(0f, 1f, 0f)
val yellow = new Color(1f, 1f, 0f)
//for(t <- tesselator.allTrapezoids) { if(debug) {
for(t <- tesselator.trapezoids) { val draw = if(drawMap) tesselator.allTrapezoids else tesselator.trapezoids
val polygon = new Polygon() for(t <- draw) {
for(v <- t.vertices) { val polygon = new Polygon()
polygon.addPoint(v.x, v.y) for(v <- t.vertices) {
} polygon.addPoint(v.x, v.y)
//g.setColor(red) }
//g.draw(polygon) 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)
g.setColor(red)
g.draw(polygon)
}
}
for(x <- tesselator.xMonoPoly) { for(x <- tesselator.xMonoPoly) {
var t = x.triangles var t = x.triangles
@ -94,6 +105,8 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
override def keyPressed(key:Int, c:Char) { override def keyPressed(key:Int, c:Char) {
if(key == 1) quit = true if(key == 1) quit = true
if(key == 57) debug = !debug
if(c == 'm') drawMap = !drawMap
} }
def testTesselator { def testTesselator {
@ -118,5 +131,39 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
tesselator.process tesselator.process
} }
def snake {
val scale = 10.0f
val displace = 100
val p1 = new Point(10,1)*scale+displace
val p2 = new Point(20,10)*scale+displace
val p3 = new Point(30,1)*scale+displace
val p4 = new Point(40,10)*scale+displace
val p5 = new Point(50,1)*scale+displace
val p6 = new Point(50,10)*scale+displace
val p7 = new Point(40,20)*scale+displace
val p8 = new Point(30,10)*scale+displace
val p9 = new Point(20,20)*scale+displace
val p10 = new Point(10,10)*scale+displace
val p11 = new Point(1,20)*scale+displace
val p12 = new Point(1,10)*scale+displace
val segments = new ArrayList[Segment]
segments += new Segment(p1, p2)
segments += new Segment(p2, p3)
segments += new Segment(p3, p4)
segments += new Segment(p4, p5)
segments += new Segment(p5, p6)
segments += new Segment(p6, p7)
segments += new Segment(p7, p8)
segments += new Segment(p8, p9)
segments += new Segment(p9, p10)
segments += new Segment(p10, p11)
segments += new Segment(p11, p12)
segments += new Segment(p12, p1)
tesselator = new Triangulator(segments)
tesselator.process
}
} }

View File

@ -89,13 +89,4 @@ class Trapezoid(val leftPoint: Point, var rightPoint: Point, val top: Segment, v
top.mPoints += leftPoint top.mPoints += leftPoint
top.mPoints += rightPoint top.mPoints += rightPoint
} }
// Clear points when dividing this trapezoid
def clear {
bottom.mPoints -= leftPoint
bottom.mPoints -= rightPoint
top.mPoints -= leftPoint
top.mPoints -= rightPoint
}
} }

View File

@ -48,7 +48,7 @@ class Triangulator(var segments: ArrayList[Segment]) {
for(s <- segments) { for(s <- segments) {
val traps = queryGraph.followSegment(s) val traps = queryGraph.followSegment(s)
// Remove trapezoids from trapezoidal Map // Remove trapezoids from trapezoidal Map
traps.foreach(t => {trapezoidalMap.remove(t); t.clear}) traps.foreach(trapezoidalMap.remove)
for(t <- traps) { for(t <- traps) {
var tList: ArrayList[Trapezoid] = null var tList: ArrayList[Trapezoid] = null
val containsP = t.contains(s.p) val containsP = t.contains(s.p)
@ -93,6 +93,7 @@ class Triangulator(var segments: ArrayList[Segment]) {
// Build a list of x-monotone mountains // Build a list of x-monotone mountains
private def createMountains { private def createMountains {
for(s <- segments) { for(s <- segments) {
println(s.mPoints.size)
if(s.mPoints.size > 2) { if(s.mPoints.size > 2) {
val mountain = new MonotoneMountain val mountain = new MonotoneMountain
// TODO: Optomize sort? The number of points should be // TODO: Optomize sort? The number of points should be