Code: Alles auswählen
Structure TPoint
x.d
y.d
EndStructure
Structure TCircle
p.TPoint
r.d
EndStructure
Procedure EinfacherKreis(*A.TPoint, *B.TPoint, *C.TPoint, *Kreis.TCircle)
Protected.TPoint p1, p2
Protected.d vB, vC, vD
p1\x = *B\x - *A\x : p1\y = *B\y - *A\y
p2\x = *C\x - *A\x : p2\y = *C\y - *A\y
vB = p1\x * p1\x + p1\y * p1\y
vC = p2\x * p2\x + p2\y * p2\y
vD = p1\x * p2\y - p1\y * p2\x
*Kreis\p\x = (p2\y * vB - p1\y * vC) / (2 * vD)
*Kreis\p\y = (p1\x * vC - p2\x * vB) / (2 * vD)
*Kreis\p\x + *A\x : *Kreis\p\y + *A\y
*Kreis\r = Sqr(Pow(*Kreis\p\x - *A\x, 2) + Pow(*Kreis\p\y - *A\y, 2))
EndProcedure
Global mousePosition.TPoint, mouseState, polygonPoints = 360
Global Dim polygonPoints.TPoint(360)
Global.i Kreis.TCircle
polygonPoints(0)\x = 100
polygonPoints(0)\y = 200
polygonPoints(1)\x = 350
polygonPoints(1)\y = 250
polygonPoints(2)\x = 200
polygonPoints(2)\y = 100
Procedure InsidePolygon(*point.TPoint, dots, Array poly.TPoint(1))
; int pnpoly(int npol, float *xp, float *yp, float x, float y)
; {
; int i, j, c = 0;
; For (i = 0, j = npol-1; i < npol; j = i++) {
; If ((((yp[i] <= y) && (y < yp[j])) ||
; ((yp[j] <= y) && (y < yp[i]))) &&
; (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
; c = !c;
; }
; Return c;
; }
Protected i=0,j,c
While i<dots
j=(i+dots-1)%dots
;Debug Str(i)+" "+Str(j)
If ( ( (poly(i)\y <= *point\y) And (*point\y < poly(j)\y) ) Or
( (poly(j)\y <= *point\y) And (*point\y < poly(i)\y) ) ) And
( *point\x < (poly(j)\x-poly(i)\x) * (*point\y-poly(i)\y) / (poly(j)\y-poly(i)\y) + poly(i)\x)
c!1
EndIf
i+1
Wend
ProcedureReturn c
EndProcedure
Procedure PointInPolygon(*polygon.TPoint, sides, *mousePosition.TPoint)
result = #False
If StartDrawing(CanvasOutput(1))
result=insidePolygon(*mousePosition,polygonPoints,polygonPoints())
For i = 0 To polygonPoints-1
Plot(polygonPoints(i)\x, polygonPoints(i)\y, RGB(0,0,0))
Next i
If result
FillArea(WindowWidth(0)/2, WindowHeight(0)/2, RGB(0,0,0), RGB(255,0,0))
Else
FillArea(WindowWidth(0)/2, WindowHeight(0)/2, RGB(255,255,255), RGB(255,255,255))
EndIf
DrawText(0, 0, Str(*mousePosition\x)+" | "+Str(*mousePosition\y))
StopDrawing()
Delay(1)
EndIf
ProcedureReturn result
EndProcedure
polygonPoints(polygonPoints)=polygonPoints(0)
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
If OpenWindow(0, 0, 0, 600, 630, "PointInPolygon()", wFlags)
TextGadget(0, 0, 0, 600, 30, "", #PB_Text_Center)
CanvasGadget(1, 0, 30, 600, 600, #PB_Canvas_Container)
;Kreis erstellen
EinfacherKreis(polygonPoints(0), polygonPoints(1), polygonPoints(2), @Kreis)
;Debug "Radius: " + Kreis\r
For i = 0 To 359
polygonPoints(i)\x = ((Kreis\r) * Sin(i)) + Kreis\p\x
polygonPoints(i)\y = ((Kreis\r) * Cos(i)) + Kreis\p\y
Next
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_CloseWindow
appQuit = 1
Case #PB_Event_Gadget
If EventGadget() = 1 And EventType() = #PB_EventType_MouseMove
mousePosition\x = GetGadgetAttribute(1, #PB_Canvas_MouseX)
mousePosition\y = GetGadgetAttribute(1, #PB_Canvas_MouseY)
If mouseState <> PointInPolygon(@polygonPoints(), polygonPoints, @mousePosition)
mouseState ! 1
If mousestate
txt$ = "Mouse is in..."
Else
txt$ = "Mouse is out..."
EndIf
SetGadgetText(0, StrD(mousePosition\x)+" | "+StrD(mousePosition\y)+" >> "+txt$)
EndIf
EndIf
EndSelect
Until appQuit
EndIf