circle center+distance

New to FreeBASIC? Post your questions here.
Post Reply
Lionheart2020
Posts: 13
Joined: May 30, 2009 18:12

circle center+distance

Post by Lionheart2020 »

hi dear freebasic friends,

I have some simple questions but I haven't idea to realize it. have tested not so many example with freebasic yet, sorry. ;)

I would like to have a geometric centre (with PSET) of a first circle and a second one. after that I want to calculate the distance between the centres, print it. then I will animate the circles and notice how the distance are changing (in metres or centimetres), don't know how to make it, but will try it. need only help with first part of my wish :)

here my try as a skeleton part:

Code: Select all

SCREEN 12

DIM AS INTEGER x,y
DIM AS LONG distance
CIRCLE(200,200),100
CIRCLE(360,360),80,4

randomize TIMER
x=INT(RND*300)+1
y=INT(RND*200)+1

PSET (x,y)

distance = SQR((x - 200) ^ 2 + (y - 200) ^ 2)

IF distance > 100 THEN
   PRINT "point not in circle"
ELSEIF distance = 100 THEN
   PRINT "point on/at circle"
ELSE
   PRINT "point in circle"
END IF

PSET (x+50,y+50),11

GETKEY
SLEEP
I am learning the best way with own freebasic example, follow a new idea, it's a challenge for me to grasp all the stuff! :) I am just a beginner with freebasic, have found a lot of example here, but would like to learn it step by step! - good evening, lionheart2020
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

>I would like to have a geometric centre (with PSET) of a first circle and a second one. after that I want to calculate the distance between the centres, print it. then I will animate the circles and notice how the distance are changing (in metres or centimetres), don't know how to make it, but will try it. need only help with first part of my wish :)

The center formula of two points on a Cartesian plane is:
( (x1+x2)/2, (y1+y2)/2 )

Can you try to rephrase your question? It is not at all what your code is about so I don't know what you really want to know.
Lionheart2020
Posts: 13
Joined: May 30, 2009 18:12

Post by Lionheart2020 »

hi agamemnus,

sorry, it was only a first test script for me how to going on with circle and pset and try to calculate distance. I never have done it before.

my simple wish to have:

a) two circles (like wheels) with a certain distance to each other, perhaps a line connecting the circles from circle1 centre1 to circle2 centre2 between it, calculating the measure between the centre1 to centre2.

b) then: the circles are moving and the distance between circle1 and circle 2 increases or decreases like an elastic band.

but for me the first step a) would be nice to create.

thank you for the formula!

hope it helps to understand more, best regards, lionheart2020
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

Code: Select all

'====================================================================
' Two circles
'====================================================================
Randomize Timer * 1e6
Dim As Integer xs, ys
Screeninfo xs, ys
Screenres xs, ys, 4 
Dim As String s

Dim As Double h = ys / 2
Dim As Double q = ys / 4

Dim As Double xc1, yc1, rc1     ' circle 1
Dim As Double xc2, yc2, rc2     ' circle 2
Dim As Double dx, dy, touch, dist  ' separation

'--------------------------------------------------------------------
Do
    Cls
    '--------------------------------------------------------------------
    ' first circle
    xc1 = Rnd * h + q
    yc1 = Rnd * h + q
    rc1 = 10 + ys * Rnd / 5
    Circle(xc1, yc1), rc1, 14
    Pset (xc1, yc1), 14
    
    '--------------------------------------------------------------------
    ' second circle
    xc2 = Rnd * h + q
    yc2 = Rnd * h + q
    rc2 = 10 + ys * Rnd / 5
    Circle(xc2, yc2), rc2, 13
    Pset (xc2, yc2), 13
    
    Line (xc1, yc1)-(xc2, yc2), 7
    
    '--------------------------------------------------------------------
    touch = rc1 + rc2       ' critical separation when touching
    
    dx = xc1 - xc2
    dy = yc1 - yc2
    dist = Sqr(dx*dx + dy*dy)
    
    Print
    Print " Press Esc to quit, any other key to repeat."
    Print
    If dist > touch Then
        Print "   Circles do not touch"
    Else
        Print "                               Circles overlap"
    End If
    
    '--------------------------------------------------------------------
    Do  ' wait for a key press
        s = Inkey
    Loop Until s <> ""
    
    If Asc(s) = 27 Then Exit Do   ' press escape to exit
    
Loop

'====================================================================
Sleep
'====================================================================
Lionheart2020
Posts: 13
Joined: May 30, 2009 18:12

Post by Lionheart2020 »

that's it ! :D great. many thanks richard.

this one was important for me:

Code: Select all

  touch = rc1 + rc2       ' critical separation when touching
   
    dx = xc1 - xc2
    dy = yc1 - yc2
    dist = Sqr(dx*dx + dy*dy)
Exactly what I was looking for. It looks so simple but I am sure I couldn't do it with my few skills. thanks for this good example and fast reply. best regards. lionheart2020 (leroy)
Post Reply