Circle collision
Author |
Message |
copthesaint
|
Posted: Mon Mar 30, 2009 12:45 pm Post subject: Circle collision |
|
|
The problem Is with drawing the black dots inside of the circle. This should be working (looking at the code)
but I cant seem to get it to work.
Help would be great and explination of what I did wrong.
Turing: | View.Set ("nobuttonbar,offscreenonly,graphics:max;max")
var MouseX, NextPoint, MouseY, MouseB : int := 20 /*Points of detection*/
var PointSx, PointSy : array 1 .. 100 of int
var InsideX, InsideY : array 1 .. 100 of boolean
var Activate : boolean
proc DrawPoint (X, Y : int)
drawfilloval (X, Y, 2, 2, blue)
end DrawPoint
fcn CircleDetect (Radius, X, Y, MouseX, MouseY : int, Active : boolean) : boolean
NextPoint := 0
for xr : (Radius * - 1) .. Radius
for yr : (Radius * - 1) .. Radius
if ((xr * xr ) + (yr * yr )) = (Radius * Radius ) then
DrawPoint (X + (xr ), Y + (yr ))
NextPoint + = 1
InsideX (NextPoint ) := false
InsideY (NextPoint ) := false
PointSx (NextPoint ) := (X ) + (xr )
PointSy (NextPoint ) := (Y ) + (yr )
end if
end for
end for
for xr : (Radius * - 1) .. Radius
for yr : (Radius * - 1) .. Radius
if PointSx (NextPoint ) < X and PointSy (NextPoint ) < Y then
if MouseX < PointSx (NextPoint ) then
InsideX (NextPoint ) := false
else
InsideX (NextPoint ) := true
end if
if MouseY < PointSx (NextPoint ) then
InsideY (NextPoint ) := false
else
InsideY (NextPoint ) := true
end if
elsif PointSx (NextPoint ) >= X and PointSy (NextPoint ) < Y then
if MouseX >= PointSx (NextPoint ) then
InsideX (NextPoint ) := false
else
InsideX (NextPoint ) := true
end if
if MouseY < PointSx (NextPoint ) then
InsideY (NextPoint ) := false
else
InsideY (NextPoint ) := true
end if
elsif PointSx (NextPoint ) < X and PointSy (NextPoint ) >= Y then
if MouseX < PointSx (NextPoint ) then
InsideX (NextPoint ) := false
else
InsideX (NextPoint ) := true
end if
if MouseY >= PointSx (NextPoint ) then
InsideY (NextPoint ) := false
else
InsideY (NextPoint ) := true
end if
elsif PointSx (NextPoint ) >= X and PointSy (NextPoint ) >= Y then
if MouseX >= PointSx (NextPoint ) then
InsideX (NextPoint ) := false
else
InsideX (NextPoint ) := true
end if
if MouseY >= PointSx (NextPoint ) then
InsideY (NextPoint ) := false
else
InsideY (NextPoint ) := true
end if
end if
end for
end for
Activate := true
for i : 1 .. NextPoint
if InsideX (i ) = false or InsideY (i ) = false then
Activate := false
end if
end for
result Activate
end CircleDetect
loop
Mouse.Where (MouseX, MouseY, MouseB )
if CircleDetect (50, 200, maxy div 2, MouseX, MouseY, false) = true then
drawfilloval (MouseX, MouseY, 2, 2, black)
elsif CircleDetect (50, 200, maxy div 2, MouseX, MouseY, false) = false then
drawfilloval (MouseX, MouseY, 2, 2, red)
end if
View.Update
end loop |
sorry the code is so long had to stretch it to help debug. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Mon Mar 30, 2009 1:32 pm Post subject: RE:Circle collision |
|
|
Your CircleDetect method is, to say the least, slightly screwy. It looks like you're trying to use triangles to detect whether the cursor is inside a polygon approximating the circle, but I could be mistaken.
If you're only planning on approximating circles, it's much easier to just use some simple arithmetic based on the mouse location and the circle-centre and radius than this method. The following returns true if the cursor is inside the circle:
Turing: | result (MouseX-X) ** 2 + (MouseY-Y) ** 2 < Radius ** 2 |
When I replace the second set of nested loops with that line above, the method appears to do what you want - it draws red dots whenever you're outside the circle, and black dots whenever you're inside.
Side note: you should only run check methods like that once; instead of:
Turing: |
if CircleDetect (50, 200, maxy div 2, MouseX, MouseY, false) = true then
drawfilloval (MouseX, MouseY, 2, 2, black)
elsif CircleDetect (50, 200, maxy div 2, MouseX, MouseY, false) = false then
drawfilloval (MouseX, MouseY, 2, 2, red)
end if
|
Turing: |
if CircleDetect (50, 200, maxy div 2, MouseX, MouseY, false) then
drawfilloval (MouseX, MouseY, 2, 2, black)
else
drawfilloval (MouseX, MouseY, 2, 2, red)
end if
|
Avoids calling the method twice, but accomplishes the same thing. Notice that we don't have to compare with true either - it's already true or false. |
|
|
|
|
|
copthesaint
|
Posted: Mon Mar 30, 2009 10:45 pm Post subject: RE:Circle collision |
|
|
Wow thanks. I Understand what you said and thanks.
I think I was kinda over thinking for the progam But again thanks.
Here is the program for anyone who may want this.
Turing: | View.Set ("nobuttonbar,offscreenonly,graphics:300;300")
var MouseX, MouseY, MouseB : int := 20
fcn CircleDetect (Radius, X, Y, MouseX, MouseY : int) : boolean
result (MouseX - X ) ** 2 + (MouseY - Y ) ** 2 < Radius ** 2
end CircleDetect
loop
Mouse.Where (MouseX, MouseY, MouseB )
if CircleDetect (50, maxx div 2, maxy div 2, MouseX, MouseY ) then
drawfilloval (MouseX, MouseY, 2, 2, black)
else
end if
drawoval (maxx div 2, maxy div 2, 50, 50, red)
View.Update
end loop
|
Ohh well at least I have learned something. |
|
|
|
|
|
DemonWasp
|
Posted: Mon Mar 30, 2009 11:55 pm Post subject: RE:Circle collision |
|
|
It's an important lesson, so let's make this abundantly clear:
Simpler is Better. |
|
|
|
|
|
|
|