Try my new website:
www.urcho.com
- the new SIMPLE social network
Sign up
|
358
members |
125
snippets
Search for:
ALL
POPULAR
File Controls
Multiplayer Code
2D Effects
3D Effects
Oldskool demos
Basic Functions
Maths/Physics
Sound
Tutorials
Misc
Username:
Password:
Sign up
Circle from 3 points
[
back
]
Author:
Andy_A
| Viewed:
242
times | Language:
BlitzPlus
| Category:
Maths/Physics
; Title: Circle From 3 Points ;Programmer: Andy Amaya ; Date: 2005.02.24 AppTitle "Circle From 3 Points" Graphics 800,600,32,2 SetBuffer BackBuffer() Dim x%(4),y%(4) ClsColor 0, 32, 96 Cls While KeyHit(1) = 0 Cls If MouseHit(1) Then numClicks% = numClicks%+1 x%(numClicks%) = MouseX() y%(numClicks%) = MouseY() If numClicks% = 3 Then For i% = 1 To 3 Color 255,0,0 Oval x%(i%)-5, y%(i%)-5, 10, 10, True Color 255,255,255 Text(5,i%*15, "x"+i%+": "+x(i%)+" y"+i%+": "+y%(i%) ) Next circ3pts(x%(1),y%(1),x%(2),y%(2),x%(3),y%(3)) Color 255,255,255 Text(400,580,"Press a key to continue. ESC to Exit",True) Flip WaitKey() ;clear out old values FlushMouse() numClicks% = 0 Dim x%(4), y%(4) End If End If If numClicks% > 0 Then For i% = 1 To numClicks% Color 255,0,0 Oval x%(i%)-5, y%(i%)-5, 10, 10, True Next End If Color 255,255,255 Text 10, 580, "Click 3 points to define circle" Flip Wend End Function circ3pts(x1%, y1%, x2%, y2%, x3%,y3%) Local temp%, oX%, oY%, nX%, nY%, i% Local ma#, mb#, cX#, cY#, radius# ;===Begin parameter validation================= ;If Line segment x1,y1 to x2,y2 is vertical, ;it will cause a divide by zero error when ;calculating the slope, so swap the ;points around to avoid the error. If x1% = x2% Then temp% = x2% ;swap (x2, x3) x2% = x3% x3% = temp% temp% = y2% ;swap (y2, y3) y2% = y3% y3% = temp% End If ;Same goes for these points If x2% = x3% Then temp% = x1% ;swap (x1, x3) x1% = x3% x3% = temp% temp% = y1% ;swap (y1, y3) y1% = y3% y3% = temp% End If ;ma And mb are the slopes of the lines for by x1,y1 to x2,y2 ;And x2,y2 to x3,y3 (respectively) ma# = Float(y2%-y1%)/Float(x2%-x1%) mb# = Float(y3%-y2%)/Float(x3%-x2%) ; DebugLog "ma# = " + ma# ; DebugLog "mb# = " + mb# ; DebugLog "mb# - ma# = " + (mb#-ma#) If mb# - ma# <> 0.0 Then ;Find center X coord using this formula cX# = ( (ma#*mb#*Float(y1%-y3%)) + (mb#*Float(x1%+x2%)) - (ma#*Float(x2%+x3%)) ) / (2.0 * (mb#-ma#)) ;Show the value of center X on screen Color 255,255,255 Text(10,80,"centerX = "+Str$(cX#)) ;Find center Y coord using this formula cY# = (-1.0/ma#) * (cX#-Float(x1%+x2%)/2.0) + (Float(y1%+y2%)/2.0) ;Show the value of center Y on screen Text(10,95,"centerY = "+Str$(cY#)) ;Plot the center of the circle using green colored dot Color 0,255,0 Oval cX#-5., cY#-5., 10, 10, True ;Since we now know the center coords just find the distance ;from the center to any of the given coords (point x1,y1 here) ;And this is the radius radius# = Sqr( (cX#-Float(x1%))*(cX#-Float(x1%)) + (cY#-Float(y1%))*(cY#-Float(y1%)) ) ;Print radius value To the screen Color 255,255,255 Text(10,110," radius = "+Str$(radius#)) ;All of the necessary info is at hand so draw the circle Color 255,255,0 r# = Floor(radius#) Oval cX#-r#, cY#-r#, r#*2.0, r#*2.0, False End If End Function
Author Comments:
Draws a circle from any 3 non-collinear points.
Login or
create an account
to comment on this snippet