Punkt im Dreieck - Algorythmus

Übersicht BlitzMax, BlitzMax NG Allgemein

Neue Antwort erstellen

juse4pro

Betreff: Punkt im Dreieck - Algorythmus

BeitragFr, Jul 23, 2010 1:21
Antworten mit Zitat
Benutzer-Profile anzeigen
Hi, ich hoffe der Titel sagts..

Ich suche ein Algorythmus, mitdem ich prüfen kann, ob ein Punkt in einem beliebigem Dreieck ist. Jede Ecke sollte durch 2D Koordinaten beschrieben werden.

juse
Portfolio |LinkedIn |XING

Vertex

BeitragFr, Jul 23, 2010 1:55
Antworten mit Zitat
Benutzer-Profile anzeigen
Bei Google gibts da massenweise an Treffern wie z. B. http://www.gamedev.net/communi..._id=295943

Edit: Und wer es in BB braucht (hatte es zu spät bemerkt, dass er es auch so hinbekommen hatte)
BlitzBasic: [AUSKLAPPEN]
Dim triangle#(2, 1), point#(1)

triangle#(0, 0) = 400.0 : triangle#(0, 1) = 150.0
triangle#(1, 0) = 200.0 : triangle#(1, 1) = 400.0
triangle#(2, 0) = 600.0 : triangle#(2, 1) = 400.0

Graphics 800, 600, 0, 2
SetBuffer BackBuffer()

While Not KeyDown(1)
Cls()

point(0) = MouseX() : point(1) = MouseY()
If TestPointInTriangle%() Then
Color 255, 0, 0
Else
Color 255, 255, 0
EndIf


DrawTestCase()
Flip()
Wend

End

Function DrawTestCase()
For I = 0 To 2
Oval triangle#(I, 0)-2, triangle#(I, 1)-2, 5, 5
Next
For I = 0 To 2
Local edgeA = I, edgeB = (I + 1) Mod 3
Line triangle#(edgeA, 0), triangle#(edgeA, 1), triangle#(edgeB, 0), triangle#(edgeB, 1)
Next

Oval point(0)-2, point(1)-2, 5, 5
End Function

Function TestPointInTriangle%()
Local b1%, b2%, b3%

b1% = Sign(point(0), point(1), triangle(0, 0), triangle(0, 1), triangle(1, 0), triangle(1, 1)) < 0.0
b2% = Sign(point(0), point(1), triangle(1, 0), triangle(1, 1), triangle(2, 0), triangle(2, 1)) < 0.0
b3% = Sign(point(0), point(1), triangle(2, 0), triangle(2, 1), triangle(0, 0), triangle(0, 1)) < 0.0

Return (b1 = b2) And (b2 = b3)
End Function

Function Sign#(x1#, y1#, x2#, y2#, x3#, y3#)
Return (x1# - x3#)*(y2# - y3#) - (x2# - x3#)*(y1# - y3#)
End Function
vertex.dreamfall.at | GitHub
  • Zuletzt bearbeitet von Vertex am Fr, Jul 23, 2010 3:03, insgesamt einmal bearbeitet

juse4pro

BeitragFr, Jul 23, 2010 2:37
Antworten mit Zitat
Benutzer-Profile anzeigen
okay das check ich nicht.. liegt wahrscheinlich liegts daran, dass ich nicht so gut C++ kann...
Aber warum übergibt er Pointer? und warum diese unter-funktion?

EDIT:
Hat sich erledigt... ist schon spät...

hier mein Ergebnis:
BlitzMax: [AUSKLAPPEN]

SuperStrict

Graphics 640,480,0,50

Type tPoint
Field x:Float, y:Float

Method Set(x:Float, y:Float)
Self.x = x
Self.y = y
EndMethod
EndType

Type tTri
Field p1:tPoint=New tPoint, p2:tPoint=New tPoint, p3:tPoint=New tPoint

Method Draw()
DrawLine(Self.p1.x, Self.p1.y, Self.p2.x, Self.p2.y)
DrawLine(Self.p2.x, Self.p2.y, Self.p3.x, Self.p3.y)
DrawLine(Self.p1.x, Self.p1.y, Self.p3.x, Self.p3.y)
EndMethod
EndType


Function Sign:Float(p1:tPoint, p2:tPoint, p3:tPoint)
Return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y)
EndFunction

Function PointInTri:Byte(pt:tPoint, tri:tTri)
Local b1:Byte, b2:Byte, b3:Byte
b1 = Sign(pt, tri.p1, tri.p2) < 0.0
b2 = Sign(pt, tri.p2, tri.p3) < 0.0
b3 = Sign(pt, tri.p3, tri.p1) < 0.0
Return ((b1 = b2) & (b2 = b3))
EndFunction



Local tri1:tTri = New tTri
tri1.p1.Set(100, 100)
tri1.p2.Set(100, 200)
tri1.p3.Set(300, 100)

While Not AppTerminate()
tri1.Draw()
Local mouse:tPoint = New tPoint
mouse.Set(MouseX(), MouseY())
DrawText("Point in Triangle? --> "+(PointInTri(mouse, tri1)), 1, 1)
Flip;Cls
Wend


Portfolio |LinkedIn |XING

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Allgemein

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group