Jump to content

Circles of given radius through two points: Difference between revisions

Circles of given radius through two points en BASIC256
(Added 11l)
(Circles of given radius through two points en BASIC256)
Line 325:
</pre>
 
=={{header|BASIC}}==
{{works with|FreeBASIC}}
<lang freebasic>Type Point
As Double x,y
Declare Property length As Double
End Type
 
=={{header|BASIC256}}==
Property point.length As Double
{{trans|Liberty BASIC}}
Return Sqr(x*x+y*y)
<lang BASIC256>
End Property
function twoCircles(x1, y1, x2, y2, radio)
if x1 = x2 and y1 = y2 then #Si los puntos coinciden
if radio = 0 then #a no ser que radio=0
print "Los puntos son los mismos "
return ""
else
print "Hay cualquier número de círculos a través de un solo punto ("; x1; ", "; y1; ") de radio "; int(radio)
return ""
end if
end if
r2 = sqr((x1-x2)^2+(y1-y2)^2) / 2 #distancia media entre puntos
if radio < r2 then
print "Los puntos están demasiado separados ("; 2*r2; ") - no hay círculos de radio "; int(radio)
return ""
end if
 
#si no, calcular dos centros
Sub circles(p1 As Point,p2 As Point,radius As Double)
cx = (x1+x2) / 2 #punto medio
Print "Points ";"("&p1.x;","&p1.y;"),("&p2.x;","&p2.y;")";", Rad ";radius
cy = (y1+y2) / 2
Var ctr=Type<Point>((p1.x+p2.x)/2,(p1.y+p2.y)/2)
#debe moverse desde el punto medio a lo largo de la perpendicular en dd2
Var half=Type<Point>(p1.x-ctr.x,p1.y-ctr.y)
dd2 = sqr(radio^2 - r2^2) #distancia perpendicular
Var lenhalf=half.length
dx1 = x2-cx #vector al punto medio
If radius<lenhalf Then Print "Can't solve":Print:Exit Sub
dy1 = y2-cy
If lenhalf=0 Then Print "Points are the same":Print:Exit Sub
dx = 0-dy1 / r2*dd2 #perpendicular:
Var dist=Sqr(radius^2-lenhalf^2)/lenhalf
dy = dx1 / r2*dd2 #rotar y escalar
Var rot= Type<Point>(-dist*(p1.y-ctr.y) +ctr.x,dist*(p1.x-ctr.x) +ctr.y)
Print print " -> CircleCirculo 1 ("&rot.x; cx+dy; ", "&rot.y; cy+dx; ")" #dos puntos, con (+)
print " -> Circulo 2 ("; cx-dy; ", "; cy-dx; ")" #y (-)
rot= Type<Point>(-(rot.x-ctr.x) +ctr.x,-((rot.y-ctr.y)) +ctr.y)
return ""
Print" -> Circle 2 ("&rot.x;","&rot.y;")"
end function
Print
End Sub
 
# p1 p2 radio
x1 = 0.1234 : y1 = 0.9876 : x2 = 0.8765 : y2 = 0.2345 : radio = 2.0
print "Puntos "; "("; x1; ","; y1; "), ("; x2; ","; y2; ")"; ", Radio "; int(radio)
print twoCircles (x1, y1, x2, y2, radio)
x1 = 0.0000 : y1 = 2.0000 : x2 = 0.0000 : y2 = 0.0000 : radio = 1.0
print "Puntos "; "("; x1; ","; y1; "), ("; x2; ","; y2; ")"; ", Radio "; int(radio)
print twoCircles (x1, y1, x2, y2, radio)
x1 = 0.1234 : y1 = 0.9876 : x2 = 0.12345 : y2 = 0.9876 : radio = 2.0
print "Puntos "; "("; x1; ","; y1; "), ("; x2; ","; y2; ")"; ", Radio "; int(radio)
print twoCircles (x1, y1, x2, y2, radio)
x1 = 0.1234 : y1 = 0.9876 : x2 = 0.8765 : y2 = 0.2345 : radio = 0.5
print "Puntos "; "("; x1; ","; y1; "), ("; x2; ","; y2; ")"; ", Radio "; int(radio)
print twoCircles (x1, y1, x2, y2, radio)
x1 = 0.1234 : y1 = 0.9876 : x2 = 1234 : y2 = 0.9876 : radio = 0.0
print "Puntos "; "("; x1; ","; y1; "), ("; x2; ","; y2; ")"; ", Radio "; int(radio)
print twoCircles (x1, y1, x2, y2, radio)
end
</lang>
 
Dim As Point p1=(.1234,.9876),p2=(.8765,.2345)
circles(p1,p2,2)
p1=Type<Point>(0,2):p2=Type<Point>(0,0)
circles(p1,p2,1)
p1=Type<Point>(.1234,.9876):p2=p1
circles(p1,p2,2)
p1=Type<Point>(.1234,.9876):p2=Type<Point>(.8765,.2345)
circles(p1,p2,.5)
p1=Type<Point>(.1234,.9876):p2=p1
circles(p1,p2,0)
 
Sleep</lang>
{{out}}
<pre>Points (0.1234,0.9876),(0.8765,0.2345), Rad 2
-> Circle 1 (-0.8632118016581893,-0.7521118016581889)
-> Circle 2 (1.863111801658189,1.974211801658189)
 
Points (0,2),(0,0), Rad 1
-> Circle 1 (0,1)
-> Circle 2 (0,1)
 
Points (0.1234,0.9876),(0.1234,0.9876), Rad 2
Points are the same
 
Points (0.1234,0.9876),(0.8765,0.2345), Rad 0.5
Can't solve
 
Points (0.1234,0.9876),(0.1234,0.9876), Rad 0
Points are the same</pre>
 
=={{header|C}}==
Line 1,345 ⟶ 1,342:
One 'circle', centred on the co-incident points. R is zero!
</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>Type Point
As Double x,y
Declare Property length As Double
End Type
 
Property point.length As Double
Return Sqr(x*x+y*y)
End Property
 
Sub circles(p1 As Point,p2 As Point,radius As Double)
Print "Points ";"("&p1.x;","&p1.y;"),("&p2.x;","&p2.y;")";", Rad ";radius
Var ctr=Type<Point>((p1.x+p2.x)/2,(p1.y+p2.y)/2)
Var half=Type<Point>(p1.x-ctr.x,p1.y-ctr.y)
Var lenhalf=half.length
If radius<lenhalf Then Print "Can't solve":Print:Exit Sub
If lenhalf=0 Then Print "Points are the same":Print:Exit Sub
Var dist=Sqr(radius^2-lenhalf^2)/lenhalf
Var rot= Type<Point>(-dist*(p1.y-ctr.y) +ctr.x,dist*(p1.x-ctr.x) +ctr.y)
Print " -> Circle 1 ("&rot.x;","&rot.y;")"
rot= Type<Point>(-(rot.x-ctr.x) +ctr.x,-((rot.y-ctr.y)) +ctr.y)
Print" -> Circle 2 ("&rot.x;","&rot.y;")"
Print
End Sub
 
 
Dim As Point p1=(.1234,.9876),p2=(.8765,.2345)
circles(p1,p2,2)
p1=Type<Point>(0,2):p2=Type<Point>(0,0)
circles(p1,p2,1)
p1=Type<Point>(.1234,.9876):p2=p1
circles(p1,p2,2)
p1=Type<Point>(.1234,.9876):p2=Type<Point>(.8765,.2345)
circles(p1,p2,.5)
p1=Type<Point>(.1234,.9876):p2=p1
circles(p1,p2,0)
 
Sleep</lang>
{{out}}
<pre>Points (0.1234,0.9876),(0.8765,0.2345), Rad 2
-> Circle 1 (-0.8632118016581893,-0.7521118016581889)
-> Circle 2 (1.863111801658189,1.974211801658189)
 
Points (0,2),(0,0), Rad 1
-> Circle 1 (0,1)
-> Circle 2 (0,1)
 
Points (0.1234,0.9876),(0.1234,0.9876), Rad 2
Points are the same
 
Points (0.1234,0.9876),(0.8765,0.2345), Rad 0.5
Can't solve
 
Points (0.1234,0.9876),(0.1234,0.9876), Rad 0
Points are the same</pre>
 
=={{header|Go}}==
2,169

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.