Visual Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 18013
Number of posts: 55386

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Draw an arc from two points and radius Posted by JohnBug on 6 Aug 2006 at 11:14 AM
Hi Everyone,
I need to draw an arc on a form only from the coordinates of the start and end points, the radius and the direction(clockwise or anticlockwise). All functions i find require the coordinates for the center of the circle.
Does anyone know how to do that?

Thanks
JohnBug
Report
Re: Draw an arc from two points and radius Posted by BitByBit_Thor on 8 Aug 2006 at 11:23 AM
This message was edited by BitByBit_Thor at 2006-8-8 15:32:55

This message was edited by BitByBit_Thor at 2006-8-8 15:22:30

: Hi Everyone,
: I need to draw an arc on a form only from the coordinates of the start and end points, the radius and the direction(clockwise or anticlockwise). All functions i find require the coordinates for the center of the circle.
: Does anyone know how to do that?
:
: Thanks
: JohnBug
:

If you have the begin and endpoints of the arc, you can draw a line between them. Put a point P on the middle of this line and then draw a second line through P, with a 90 degree angle on the first line. Call this new line l.
All the possible points for the center of the circle will be on this line l.
Now you need to draw a circle from either the begin point or end point with the given radius and the intersections between this circle and the new line are the possible circle centers (in most cases there will be 2 points).

Translating this story in the calculations:
Begin point B(a, b)
End point E(f, g)

Middle of the line P( (f+a)/2, (b+g)/2 )
Angle of the line in y/x: r1 = (g-b) / (f-a)

Then the line l (90 degrees on this line) has:
Angle of l in y/x: r2 = -1 / r1 = (a-f) / (g-b)
Given that l goes through P we can find a function y for l:
y = r*x + c --> y = (a-f)/(g-b) * x + c

Inserting P( (f+a)/2, (b+g)/2 ) into the equation makes:
(b+g)/2 = (a-f)/(g-b) * (f+a)/2 + c
c = (b+g)/2 - (a-f)(f+a)/2(g-b)

Making l:
y = (f-a)/(g-b) * x + (b+g)/2 - (a-f)(a+f)/2(g-b)
(stay with me here ... it looks worse than it is)

We are searching a point M(p, q) on l where has a distance radius to B or E (begin or end point).

M(p, q) can be written as:
M(x, (a-f)/(g-b) * x + (b+g)/2 - (a-f)(a+f)/2(g-b))

With B(a, b):

(x - a) + ((a-f)/(g-b) * x + (b+g)/2 - (a-f)(a+f)/2(g-b) - b) = radius


Well ... to solve this is going to take a while! :)
Unfortunately, I don't have enough time to finish the calculation right now (already started working it out on paper). I'll come back later today and edit the result into this post.

Well uhm :) You don't even wanna see what it's going to become. I gave up working it out fully :P

Anyway, as far as I got (and it's a miracle if I did it without mistakes!) is:

I don't know if you're familair with what we here call the 'abc'-formula? It's the formula to solve the equation: Ax + Bx + C = 0

Basically, I found such an equation with:

A = 1 + (a-f)/(g-b)
B = 0.5(a-f) - 0.5(a+f)(a-f)/(g-b)
C = 0.25 - radius + a + b + 0.25(b+g) + 0.25(a+f)(a-f)/(g-b) - 0.5b(b+g)

So following the 'abc-formula' further yields:
D = B - 4AC
You can do a check here. For any valid solution (eg for any arc that can be drawn) D will have to be equal to or larger than 0. If it's not for any situation, then either there is no possible arc, or I screwed up somewhere :P

De x coordinates of the centers of the circle are as following:
x1 = (-B + Sqr(D)) / 2A
x2 = (-B - Sqr(D)) / 2A

The y coordinates are when you insert these x values into the equation for the line l:

l: y = (a-f)/(g-b) * x + (b+g)/2 - (a-f)(a+f)/2(g-b)

y1 = (a-f)/(g-b) * x1 + (b+g)/2 - (a-f)(a+f)/2(g-b)
y2 = (a-f)/(g-b) * x2 + (b+g)/2 - (a-f)(a+f)/2(g-b)

So M1(x1, y1) and M2(x2, y2)
:)

Since A, B and C are too 'sick', the best thing to do in VB6 is to make double variables and calculate A, B and C. Then with these double values calculate D and then x1 and x2 and after that y1 and y2.

Ok ... it's a tough story :) I suggest you test it for errors by programming it and seeing if the found circle center coordinates are correct. If not, please contact me :) If there's anything you don't understand don't hesitate to ask... I did my best at explaining and undoubtedly failed misserable at it :D

Best Regards,
Richard
Report
Wow!! Richard ( BitByBit_Thor ), that looked complicated. Posted by DrMarten on 9 Aug 2006 at 12:52 AM
This message was edited by DrMarten at 2006-8-9 1:14:16


Hi,

I was starting to think along the same lines as you.
By the way do you have an A level or degree in maths or something?

I came up with the following.

If point1 coordinates are x1,y1
If point2 coordinates are x2,y2
If circle/arc centre coordinates are cx,cy then>>

cx=(x1+x2)/2
cy=(y1+y2)/2

Now for distances and angles>>

Angle1 is the angle to point1
Angle2 is the angle to point2

Angle1 is given by>>

Angle1=ArcSin( (y1-cy) / ( x1-cx) ) 'Opposite/ajacent

and Angle2 by>>

Angle2=ArcSin( (y2-cy) / ( x2-cx) ) 'Opposite/ajacent


I've been looking up code to do ArcSin ( or Inverse Sine ) function
on GOOGLE.

Anyway once i get the angle values and convert to RADIANS by dividing
the angles by the following number.>>
57.2957795 which is also equal to 360/(2*PI)

and the RADIUS=SQR( ((x1-cx)^2)) + ((y1-cy)^2) )

you can then say>>

SORRY THIS CODE IS FOR VB.NET
DOWNLOAD THE EXPRESS EDITION for FREE!!
( WHICH THIS CODE SHOULD WORK WITHIN ) HERE>>

http://msdn.microsoft.com/vstudio/express/vb/


        'cx and cy are the centre coordinates of the circle.
        Dim cx As Integer
        Dim cy As Integer
        'x2 and y2 are the plotted positions that are calculated.
        Dim x2 As Integer
        Dim y2 As Integer
        Dim Radius As Double
        Dim PI As Double
        'Just a loop Count variable to go plot each point
        'on the arc or circle.
        Dim Count As Double
        'Set up a white PEN of width=1
        Dim aPen As New Pen(Color.White, 1)
        Radius = SQR( ((x1-cx)^2)) + ((y1-cy)^2) )
        PI = Math.PI
     
        For Count = Angle1 To Angle2 Step 0.05 'Can be any step value.
            x2 = Math.Round(Radius * Math.Sin(Count) + cx, 3)
            y2 = Math.Round(Radius * Math.Cos(Count) + cy, 3)

           pictureBox1.CreateGraphics.DrawLine(aPen, x2, y2, x2 + 1, y2)

        Next Count


I know this method is not "ideal" for some people as i'm not using
SetPixel, but to do that I believe you need to load a bitmap first.
With the above METHOD you can plot on the FORM itself.
To do this i think the last line needs to be changed within
the FOR NEXT loop to>>

Me.CreateGraphics.DrawLine(aPen, x2, y2, x2 + 1, y2)


To go backwards (anti-clockwise), put a minus sign in either equation in the code above so it reads as>>

x2 = Math.Round(-Radius * Math.Sin(Count) + cx, 3)
'or but not in both
y2 = Math.Round(-Radius * Math.Cos(Count) + cy, 3)

You could multiply the radius by minus 1.

I'm off to give it a try....


Regards,

Dr M.

P.S. Richard, i followed the bit about a right angled line for
all of the points to plot from but the arc from any other point, other than the centre, would be a partial ellipse, sometimes
a diagonal ellipse.

Report
Re: Wow!! Richard ( BitByBit_Thor ), that looked complicated. Posted by BitByBit_Thor on 9 Aug 2006 at 5:37 AM
: Hi,
:
: I was starting to think along the same lines as you.
: By the way do you have an A level or degree in maths or something?
:

:) Nope ... just secondary school for now :) Going to college next year! ^^

: I came up with the following.
:
: If point1 coordinates are x1,y1
: If point2 coordinates are x2,y2
: If circle/arc centre coordinates are cx,cy then>>
:
: cx=(x1+x2)/2
: cy=(y1+y2)/2
:


Yeah I was thinking about this solution too - had the message already posted when I quickly clicked the delete button. Why? If I understand you right then this method works fine for when you are trying to draw half a circle. Perhaps he meant to create half a circle all along (I'm not really sure what the definition of arc is :) ) and if so my post was WAY too complicated :P Then this will work fine.
However, if you just want, say a quarter of a circle, then you can try but this won't work: draw it out on a piece of paper and see where your center is placed: the center is placed as such that the two points become half a circle, but not a quarter.

: P.S. Richard, i followed the bit about a right angled line for
: all of the points to plot from but the arc from any other point, other than the centre, would be a partial ellipse, sometimes
: a diagonal ellipse.
:

Yeah I know. So that's why I continue the calculation to get the exact point on the line where the arc would be a partial circle. I do it this way, because there is no easy way (as far as I know) to equate two circles:
(x - a) + (y - b) = r
With center M(a, b) and radius r
y = b + Sqr(r - (x-a))
-and
y = b - Sqr(r - (x-a))
(each of these formula's is half a circle)

If you try and equate one of these y's with another circle's y (with the same radius) you'll get:
M1(a, b) and M2(c, d)
y1 = b + Sqr(r1 - (x-a))
y2 = d + Sqr(r2 - (x-c))

y1 = y2 -->

b + Sqr(r1 - (x-a)) = d + Sqr(r2 - (x-c))
I don't know how to solve this equation, even when r1 = r2.

So that's why I took the detour saying that all the centers had to be on that line l. Making the equation (unfortunately not easier) but atleast solvable :)

Best Regards,
Richard

Report
About>> b + Sqr(r1² - (x-a)²) = d + Sqr(r2² - (x-c)²) Posted by DrMarten on 9 Aug 2006 at 10:27 AM
This message was edited by DrMarten at 2006-8-9 10:29:31

Hi,

b + Sqr(r1 - (x-a)) = d + Sqr(r2 - (x-c))

If you "square" both sides of the equation you get>>

(b^2) + (r1^2 - (x-a)^2) = (d^2) + (r2^2 - (x-c)^2) >>

becomes>>

(b^2) + (r1^2)- (x^2) -2xa - (a^2) = (d^2) + (r2^2) - (x^2) -2xc - (c^2)

If r1 and r2 are equal then>>

(b^2) - (x^2) -2xa - (a^2) = (d^2) - (x^2) -2xc - (c^2)

the ( x^2 ) also cancel each other giving>>

(b^2) -2xa - (a^2) = (d^2) -2xc - (c^2)



Any help?

Can you explain each term in your equations please BitByBit_Thor?

I.E. the a, b, c and d. What are they?
How do you get " " on a keyboard too please?
The above one was PASTED.


Regards,

Dr M.

Report
Re: About>> b + Sqr(r1² - (x-a)²) = d + Sqr(r2² - (x-c)²) Posted by BitByBit_Thor on 10 Aug 2006 at 7:03 AM
: This message was edited by DrMarten at 2006-8-9 10:29:31

: Hi,
:
: b + Sqr(r1 - (x-a)) = d + Sqr(r2 - (x-c))
:
: If you "square" both sides of the equation you get>>
:
Yeah I so wish it did:

( b + Sqr(r1 - (x-a)) ) = b + r1 - (x-a) + 2b*Sqr(r1 - (x-a))
And there we have the root, still alive.
Think about it:
(5 + 1) = 6 = 36
This is 5 + 1 + 2*5*1 = 25 + 1 + 10 = 36
NOT 5 + 1 = 26
So you can't square away the root... thus no solution.

:
: Any help?
:
: Can you explain each term in your equations please BitByBit_Thor?
:
: I.E. the a, b, c and d. What are they?
Well the center of circle one is M1(a, b) and of circle two M2(c, d)
r is the radius, and x and y are the normal grid axes.
Thus a, b, c and d are the x and y coordinates of circle one and two respectively.

: How do you get " " on a keyboard too please?
: The above one was PASTED.

^^ Alt+2 =

----------------------------------

So we need input from the person that asked the question:
What is an arc? Is it half a circle or only part of a circle?

If it's half a circle, see Dr Marten's first post.
If it's part of a circle, unfortunately see my first post :P

Best Regards,
Richard

Report
Re: About>> b + Sqr(r1² - (x-a)²) = d + Sqr(r2² - (x-c)²) Posted by JohnBug on 10 Aug 2006 at 3:47 PM
: : This message was edited by DrMarten at 2006-8-9 10:29:31

: : Hi,
: :
: : b + Sqr(r1 - (x-a)) = d + Sqr(r2 - (x-c))
: :
: : If you "square" both sides of the equation you get>>
: :
: Yeah I so wish it did:
:
: ( b + Sqr(r1 - (x-a)) ) = b + r1 - (x-a) + 2b*Sqr(r1 - (x-a))
: And there we have the root, still alive.
: Think about it:
: (5 + 1) = 6 = 36
: This is 5 + 1 + 2*5*1 = 25 + 1 + 10 = 36
: NOT 5 + 1 = 26
: So you can't square away the root... thus no solution.
:
: :
: : Any help?
: :
: : Can you explain each term in your equations please BitByBit_Thor?
: :
: : I.E. the a, b, c and d. What are they?
: Well the center of circle one is M1(a, b) and of circle two M2(c, d)
: r is the radius, and x and y are the normal grid axes.
: Thus a, b, c and d are the x and y coordinates of circle one and two respectively.
:
: : How do you get " " on a keyboard too please?
: : The above one was PASTED.
:
: ^^ Alt+2 =
:
: ----------------------------------
:
: So we need input from the person that asked the question:
: What is an arc? Is it half a circle or only part of a circle?
:
: If it's half a circle, see Dr Marten's first post.
: If it's part of a circle, unfortunately see my first post :P
:
: Best Regards,
: Richard
:
:
Hi everyone,

Sorry i was out for a couple of days.
You guys really lost me here. I'm not very good with maths and geometry so i need to read all this carefully. To answer Richard's question it's only part of a circle so the center will not be in the middle of the two points.
Thanks a lot for the inputs.

Regards
JohnBug

Report
Re: (Yeah I so wish it did.) To Richard > BitByBit_Thor. Posted by DrMarten on 11 Aug 2006 at 12:38 AM

Hi,

What i was getting at Richard is creating a formula from another
by trying to eliminate terms such as square roots or indices such as
x^2

If you take as an example>>

2+sqr(9) = 3+sqr(4)

both sides equate to 5.

Now square each of the terms separately above and you get>>

(2^2) + 9 = (3^2) + 4 equating to>>

4+9 = 9+4
and both sides equal 13


I'm not sure if this works for ALL equations but if one of the
terms equates or is equal to 1 i know it doesn't as in this
example>>

2+SQR(9)=1+SQR(16)

Both sides equal 5 however

(2^2)+9DOES NOT EQUAL (1^2)+16
I.E.
4+9 DOES NOT EQUAL 1+16
13 DOES NOT EQUAL 17


Regards,

Dr M.




Report
Ooops my 1st post was a bit wrong. This is amended. Posted by DrMarten on 11 Aug 2006 at 12:51 AM
This message was edited by DrMarten at 2006-8-11 1:7:30


: Hi,
:
: I was starting to think along the same lines as you.
: By the way do you have an A level or degree in maths or something?
:
: I came up with the following.
:
: If point1 coordinates are x1,y1
: If point2 coordinates are x2,y2
: If circle/arc centre coordinates are cx,cy then>>
:
: cx=(x1+x2)/2
: cy=(y1+y2)/2
:

: Now for distances and angles>>
:
: Angle1 is the angle to point1
: Angle2 is the angle to point2
:
: Angle1 is given by>>
:
: Angle1=Math.Atan( (y1-cy) / ( x1-cx) ) 'Opposite/ajacent
:

: and Angle2 by>>
:
: Angle2=Math.Atan( (y2-cy) / ( x2-cx) ) 'Opposite/ajacent
:


: and the RADIUS=SQR( ((x1-cx)^2)) + ((y1-cy)^2) )
:
: you can then say>>
:
: SORRY THIS CODE IS FOR VB.NET
: DOWNLOAD THE EXPRESS EDITION for FREE!!
: ( WHICH THIS CODE SHOULD WORK WITHIN ) HERE>>
:

: http://msdn.microsoft.com/vstudio/express/vb/
:

:

:
'Sorry this only works for half a circle.
         'cx and cy are the centre coordinates of the circle.
         Dim cx As Integer
         Dim cy As Integer
         'x2 and y2 are the plotted positions that are calculated.
         Dim x2 As Integer
         Dim y2 As Integer
         Dim Radius As Double
         Dim PI As Double
         'Just a loop Count variable to go plot each point
         'on the arc or circle.
         Dim Count As Double
Dim Angle1 As Double
Dim Angle2 As Double
Angle1=Math.Atan( ((y1-cy) / ( x1-cx)) /(180/Pi)  )'Converted to radians
Angle2=Math.Atan( ((y2-cy) / ( x2-cx))/(180/Pi)   )'Converted to radians
         'Set up a white PEN of width=1
         Dim aPen As New Pen(Color.White, 1)
         Radius = SQR( ((x1-cx)^2)) + ((y1-cy)^2) )
         PI = Math.PI
      
         For Count = Angle1 To Angle2 Step 0.05 'Can be any step value.
             x2 = Math.Round(Radius * Math.Sin(Count) + cx, 3)
             y2 = Math.Round(Radius * Math.Cos(Count) + cy, 3)
 
            pictureBox1.CreateGraphics.DrawLine(aPen, x2, y2, x2 + 1, y2)
 
         Next Count


: I know this method is not "ideal" for some people as i'm not using
: SetPixel, but to do that I believe you need to load a bitmap first.
: With the above METHOD you can plot on the FORM itself.
: To do this i think the last line needs to be changed within
: the FOR NEXT loop to>>
:
: Me.CreateGraphics.DrawLine(aPen, x2, y2, x2 + 1, y2)
:

:
: To go backwards (anti-clockwise), put a minus sign in either equation in the code above so it reads as>>
:
: x2 = Math.Round(-Radius * Math.Sin(Count) + cx, 3)
: 'or but not in both
: y2 = Math.Round(-Radius * Math.Cos(Count) + cy, 3)
:

: You could multiply the radius by minus 1.
:
: I'm off to give it a try....
:
:
: Regards,
:
: Dr M.
:
: P.S. Richard, i followed the bit about a right angled line for
: all of the points to plot from but the arc from any other point, other than the centre, would be a partial ellipse, sometimes
: a diagonal ellipse.
______________________________________________________________________

Hi,

My original post above is now amended as Opposite/Adjacent is Tan not
Sine as i originally put. Ooops....

Regards,

Dr M.



Report
To Richard. Would knowing this help re: 2 circles? AMENDED. Posted by DrMarten on 11 Aug 2006 at 5:34 AM
This message was edited by DrMarten at 2006-8-12 0:26:44


If a circle call it c1 is plotted with

x1=R*sin(a)+x
y1=R*cos(a)+y

where x,y are the centre and r the radius and (a) is the same angle
for any point then if you imagine a 2nd circle with radius r2
at the same centre the formulae become>>

x1=R*sin(a)+x
newR=R*(r2/R)
y1=newR*cos(a)+y

or >>
x1=R*sin(a)+x
y1=R*(r2/R)*cos(a)+y

is the same.
This results in a horizontal ellipse if r2<R or a vertical one
if r2>R.

For the same centre a diagonal ellipse if plotted as>>

x1=R*sin(a)+x
y1=R*(r2/R)*cos(a+b)+y

where b is any angle.

______________________________________________________________________

Another thing about an ellipse is it can be plotted from
2 centres or locci, i believe they are called.

Call the loccii centres w,x and y,z

Any point on the ellipse equates to C=a+b where
C is always constant and>>>

a is the distance from w,x ( locci1 )
b is the distance from y,z ( locci2 )

You can do this with a piece of string which is loose between
2 points, C being the length of the string.
Fasten with drawing pins on a suitable surface and if you pull
the string tight away from the 2 points with a pencil and move around
the result of the plot is an ellipse.

See the sketch at>>
http://i13.photobucket.com/albums/a272/u-might-want-this/EllipseSketch.jpg which shows what i'm talking about.
r2 is the distance from the centre to the ellipse cicumference
not the full yellow line length,
R is the distance from the centre to the ellipse cicumference
and not the full white horizontal line length.

I'd love to know how to calculate the w,x and y,z
coordinates of the 2 locci though....


Regards,

Dr M.

Report
Re: To Richard. Would knowing this help re: 2 circles? AMENDED. Posted by BitByBit_Thor on 13 Aug 2006 at 1:53 PM
: This message was edited by DrMarten at 2006-8-12 0:26:44

:
: If a circle call it c1 is plotted with
:
: x1=R*sin(a)+x
: y1=R*cos(a)+y
:
: where x,y are the centre and r the radius and (a) is the same angle
: for any point then if you imagine a 2nd circle with radius r2
: at the same centre the formulae become>>
:
: x1=R*sin(a)+x
: newR=R*(r2/R)
: y1=newR*cos(a)+y
:
: or >>
: x1=R*sin(a)+x
: y1=R*(r2/R)*cos(a)+y
:
: is the same.
: This results in a horizontal ellipse if r2<R or a vertical one
: if r2>R.
:
: For the same centre a diagonal ellipse if plotted as>>
:
: x1=R*sin(a)+x
: y1=R*(r2/R)*cos(a+b)+y
:
: where b is any angle.
:
: ______________________________________________________________________
:
: Another thing about an ellipse is it can be plotted from
: 2 centres or locci, i believe they are called.
:
: Call the loccii centres w,x and y,z
:
: Any point on the ellipse equates to C=a+b where
: C is always constant and>>>
:
: a is the distance from w,x ( locci1 )
: b is the distance from y,z ( locci2 )
:
: You can do this with a piece of string which is loose between
: 2 points, C being the length of the string.
: Fasten with drawing pins on a suitable surface and if you pull
: the string tight away from the 2 points with a pencil and move around
: the result of the plot is an ellipse.
:
: See the sketch at>>
: http://i13.photobucket.com/albums/a272/u-might-want-this/EllipseSketch.jpg which shows what i'm talking about.
: r2 is the distance from the centre to the ellipse cicumference
: not the full yellow line length,
: R is the distance from the centre to the ellipse cicumference
: and not the full white horizontal line length.
:
: I'd love to know how to calculate the w,x and y,z
: coordinates of the 2 locci though....
:
:
: Regards,
:
: Dr M.
:
:

I must be missing something here, but uhmm what's your point?

I once knew the formula for the ellips, but kinda forgot it now (too bad).

Best Regards,
Richard

Report
Re: About>> b + Sqr(r1² - (x-a)²) = d + Sqr(r2² - (x-c)²) Posted by BitByBit_Thor on 13 Aug 2006 at 2:09 PM
: Hi everyone,
:
: Sorry i was out for a couple of days.
: You guys really lost me here. I'm not very good with maths and geometry so i need to read all this carefully. To answer Richard's question it's only part of a circle so the center will not be in the middle of the two points.
: Thanks a lot for the inputs.
:
: Regards
: JohnBug
:
:

Yeah really sorry to complicate matters like this, but it's just not an easy question ;)

I'm affraid you're stuck with my original post.

Basically, what you need to do is this:

You'll get the start and end points along with the radius from the user.

The coordinates will look like this:
Begin point B(a, b)
End point E(f, g)
Radius r

(When programming the code, it's advisable to give them names like startX, startY, endX, endY and Radius, but for now, let's use these simple letters.)

Then calculate the following values using the given formula's:

A = 1 + (a-f)/(g-b)
B = 0.5(a-f) - 0.5(a+f)(a-f)/(g-b)
C = 0.25 - radius + a + b + 0.25(b+g) + 0.25(a+f)(a-f)/(g-b) - 0.5b(b+g)

(Here also, I advise you to use names like formuA, formuB and formuC for the capital A, B and C. I'd make the variables Doubles.)
For example, in code A would look like this (using the suggested variable names):
formuA = 1 + ((beginX - endX) / (endY - beginY))^2


Continue with calculating D:
D = B - 4AC

formuD = formuB^2 - 4*formuA*formuC
'You should check here if D yields a usable value
If (formuD < 0) Then
  MsgBox "The inputted values are not part of a circle with " & _
    "the specified radius"
  'Stop the process here, for instance by exiting the function
  Exit Sub
End If


Now you can calculate the x and y values of the circle's center:

x1 = (-B + Sqr(D)) / 2A
y1 = (a-f)/(g-b) * x1 + (b+g)/2 - (a-f)(a+f)/2(g-b)

x2 = (-B - Sqr(D)) / 2A
y2 = (a-f)/(g-b) * x2 + (b+g)/2 - (a-f)(a+f)/2(g-b)

A part of it in code (too lazy to translate it all ;) )
circle1_x = (Sqr(formuD) - formuB)) / (2*formuA)
circle1_y = ((beginX - endX) / (endY - beginY)) * circle1_x + ...

And so on for the rest of the formula.

Now you have an x and y value of the circle's center to use with the other formula's.
You can use either of the circle centers' x and y. I suggest that in code you only calculate x1 and y1 and use those.

Best Regards,
Richard

Report
Re: About>> b + Sqr(r1² - (x-a)²) = d + Sqr(r2² - (x-c)²) Posted by JohnBug on 13 Aug 2006 at 7:07 PM
: : Hi everyone,
: :
: : Sorry i was out for a couple of days.
: : You guys really lost me here. I'm not very good with maths and geometry so i need to read all this carefully. To answer Richard's question it's only part of a circle so the center will not be in the middle of the two points.
: : Thanks a lot for the inputs.
: :
: : Regards
: : JohnBug
: :
: :
:
: Yeah really sorry to complicate matters like this, but it's just not an easy question ;)
:
: I'm affraid you're stuck with my original post.
:
: Basically, what you need to do is this:
:
: You'll get the start and end points along with the radius from the user.
:
: The coordinates will look like this:
: Begin point B(a, b)
: End point E(f, g)
: Radius r
:
: (When programming the code, it's advisable to give them names like startX, startY, endX, endY and Radius, but for now, let's use these simple letters.)
:
: Then calculate the following values using the given formula's:
:
: A = 1 + (a-f)/(g-b)
: B = 0.5(a-f) - 0.5(a+f)(a-f)/(g-b)
: C = 0.25 - radius + a + b + 0.25(b+g) + 0.25(a+f)(a-f)/(g-b) - 0.5b(b+g)
:
: (Here also, I advise you to use names like formuA, formuB and formuC for the capital A, B and C. I'd make the variables Doubles.)
: For example, in code A would look like this (using the suggested variable names):
:
: formuA = 1 + ((beginX - endX) / (endY - beginY))^2
: 

:
: Continue with calculating D:
: D = B - 4AC
:
:
: formuD = formuB^2 - 4*formuA*formuC
: 'You should check here if D yields a usable value
: If (formuD < 0) Then
:   MsgBox "The inputted values are not part of a circle with " & _
:     "the specified radius"
:   'Stop the process here, for instance by exiting the function
:   Exit Sub
: End If
: 

:
: Now you can calculate the x and y values of the circle's center:
:
: x1 = (-B + Sqr(D)) / 2A
: y1 = (a-f)/(g-b) * x1 + (b+g)/2 - (a-f)(a+f)/2(g-b)
:
: x2 = (-B - Sqr(D)) / 2A
: y2 = (a-f)/(g-b) * x2 + (b+g)/2 - (a-f)(a+f)/2(g-b)
:
: A part of it in code (too lazy to translate it all ;) )
:
: circle1_x = (Sqr(formuD) - formuB)) / (2*formuA)
: circle1_y = ((beginX - endX) / (endY - beginY)) * circle1_x + ...
: 

: And so on for the rest of the formula.
:
: Now you have an x and y value of the circle's center to use with the other formula's.
: You can use either of the circle centers' x and y. I suggest that in code you only calculate x1 and y1 and use those.
:
: Best Regards,
: Richard
:
:
Hi,

Looks clearer now.
I'm gonna try this and let you know.
Once it's finish i'll post the app in case others need it.

Thanks again
Jean


Report
Re: To Richard. My point was this>> Posted by DrMarten on 14 Aug 2006 at 8:05 AM
This message was edited by DrMarten at 2006-8-14 8:9:9

: :
: : If a circle call it c1 is plotted with
: :
: : x1=R*sin(a)+x
: : y1=R*cos(a)+y
: :
: : where x,y are the centre and r the radius and (a) is the same angle
: : for any point then if you imagine a 2nd circle with radius r2
: : at the same centre the formulae become>>
: :
: : x1=R*sin(a)+x
: : newR=R*(r2/R)
: : y1=newR*cos(a)+y
: :
: : or >>
: : x1=R*sin(a)+x
: : y1=R*(r2/R)*cos(a)+y
: :
: : is the same.
: : This results in a horizontal ellipse if r2<R or a vertical one
: : if r2>R.
: :
: : For the same centre a diagonal ellipse if plotted as>>
: :
: : x1=R*sin(a)+x
: : y1=R*(r2/R)*cos(a+b)+y
: :
: : where b is any angle.
: :
: : ______________________________________________________________________
: :
: : Another thing about an ellipse is it can be plotted from
: : 2 centres or locci, i believe they are called.
: :
: : Call the loccii centres w,x and y,z
: :
: : Any point on the ellipse equates to C=a+b where
: : C is always constant and>>>
: :
: : a is the distance from w,x ( locci1 )
: : b is the distance from y,z ( locci2 )
: :
: : You can do this with a piece of string which is loose between
: : 2 points, C being the length of the string.
: : Fasten with drawing pins on a suitable surface and if you pull
: : the string tight away from the 2 points with a pencil and move around
: : the result of the plot is an ellipse.
: :
: : See the sketch at>>
: : http://i13.photobucket.com/albums/a272/u-might-want-this/EllipseSketch.jpg which shows what i'm talking about.
: : r2 is the distance from the centre to the ellipse cicumference
: : not the full yellow line length,
: : R is the distance from the centre to the ellipse cicumference
: : and not the full white horizontal line length.
: :
: : I'd love to know how to calculate the w,x and y,z
: : coordinates of the 2 locci though....
: :
: :
: : Regards,
: :
: : Dr M.
: :
: :
:
: I must be missing something here, but uhmm what's your point?
:
: I once knew the formula for the ellips, but kinda forgot it now (too bad).
:
: Best Regards,
: Richard

=====================================================================

Hi Richard,

I was wondering if knowing the methods to plot a circle or an ellipse
that i have posted here can in some way be applied with your posted
formulas to plot an arc from a circle or an ellipse between
any two points....as per the original question at all please?
Of course this would put the mid line between the two points at any
angle depending on the ratio of the ellipse and whether it is a
horizontal, vertical or a diagonal ellipse.

That may be getting very complicated though.

The post was a hope about trying to make things simpler here too.



Regards,

Dr M.

Report
Re: About>> b + Sqr(r1² - (x-a)²) = d + Sqr(r2² - (x-c)²) Posted by JohnBug on 14 Aug 2006 at 8:11 AM
: : : Hi everyone,
: : :
: : : Sorry i was out for a couple of days.
: : : You guys really lost me here. I'm not very good with maths and geometry so i need to read all this carefully. To answer Richard's question it's only part of a circle so the center will not be in the middle of the two points.
: : : Thanks a lot for the inputs.
: : :
: : : Regards
: : : JohnBug
: : :
: : :
: :
: : Yeah really sorry to complicate matters like this, but it's just not an easy question ;)
: :
: : I'm affraid you're stuck with my original post.
: :
: : Basically, what you need to do is this:
: :
: : You'll get the start and end points along with the radius from the user.
: :
: : The coordinates will look like this:
: : Begin point B(a, b)
: : End point E(f, g)
: : Radius r
: :
: : (When programming the code, it's advisable to give them names like startX, startY, endX, endY and Radius, but for now, let's use these simple letters.)
: :
: : Then calculate the following values using the given formula's:
: :
: : A = 1 + (a-f)/(g-b)
: : B = 0.5(a-f) - 0.5(a+f)(a-f)/(g-b)
: : C = 0.25 - radius + a + b + 0.25(b+g) + 0.25(a+f)(a-f)/(g-b) - 0.5b(b+g)
: :
: : (Here also, I advise you to use names like formuA, formuB and formuC for the capital A, B and C. I'd make the variables Doubles.)
: : For example, in code A would look like this (using the suggested variable names):
: :
: : formuA = 1 + ((beginX - endX) / (endY - beginY))^2
: : 

: :
: : Continue with calculating D:
: : D = B - 4AC
: :
: :
: : formuD = formuB^2 - 4*formuA*formuC
: : 'You should check here if D yields a usable value
: : If (formuD < 0) Then
: :   MsgBox "The inputted values are not part of a circle with " & _
: :     "the specified radius"
: :   'Stop the process here, for instance by exiting the function
: :   Exit Sub
: : End If
: : 

: :
: : Now you can calculate the x and y values of the circle's center:
: :
: : x1 = (-B + Sqr(D)) / 2A
: : y1 = (a-f)/(g-b) * x1 + (b+g)/2 - (a-f)(a+f)/2(g-b)
: :
: : x2 = (-B - Sqr(D)) / 2A
: : y2 = (a-f)/(g-b) * x2 + (b+g)/2 - (a-f)(a+f)/2(g-b)
: :
: : A part of it in code (too lazy to translate it all ;) )
: :
: : circle1_x = (Sqr(formuD) - formuB)) / (2*formuA)
: : circle1_y = ((beginX - endX) / (endY - beginY)) * circle1_x + ...
: : 

: : And so on for the rest of the formula.
: :
: : Now you have an x and y value of the circle's center to use with the other formula's.
: : You can use either of the circle centers' x and y. I suggest that in code you only calculate x1 and y1 and use those.
: :
: : Best Regards,
: : Richard
: :
: :
: Hi,
:
: Looks clearer now.
: I'm gonna try this and let you know.
: Once it's finish i'll post the app in case others need it.
:
: Thanks again
: Jean
:
:
:
Hi,
Yes it's me again
I tried your formulas and I must be doing something wrong because it doesn't yield the right results.
I tried with the following values:
start_x=100
start_y=100
end_x=200
end_y=200
radius=150

This should put the center at approx. x=56 and y=243 (measured on a paper). The results I get are x=100 and y=1500050.

Here's my code. Do you see anything wrong?

FormuA = 1 + (Start_x - End_x) ^ 2 / (End_y - Start_y) ^ 2

FormuB = 0.5 * (Start_x - End_x) - 0.5 * (Start_x + End_x) * (Start_x - End_x) ^ 2 / (End_y - Start_y) ^ 2

FormuC = 0.25 - Radius ^ 2 + Start_x ^ 2 + Start_y ^ 2 + 0.25 * (Start_y + End_y) ^ 2 + 0.25 * (Start_x + End_x) ^ 2 * (Start_x - End_x) ^ 2 / (End_y - Start_y) ^ 2 - 0.5 * Start_y * (Start_y - End_y)

FormuD = FormuB ^ 2 - 4 * FormA * FormuC

Center_x1 = (Sqr(FormuD) - FormuB) / (2 * FormuA)
Center_y1 = ((Start_x - End_x) / (End_y - Start_y)) * Center_x1 + (Start_y + End_y) / 2 - (Start_x - End_x) * (Start_x + End_x) / 2 * (End_y - Start_y)

Thanks for the great help
JohnBug

Report
Re: To Richard. My point was this>> Posted by BitByBit_Thor on 14 Aug 2006 at 12:21 PM
Ahh right :)

Still wondering why you are talking about an ellips here? Must've missed something I guess, cause I don't see where an ellips comes in when drawing a part of a circle.

Anyway, like I said my first thought was to use the circle function and then continue from there. That, however, was not really successful.

You'll end up with just as difficult calculations as mine - and they're even harder to solve.

The problem is the Sqr that always occurs, or the fact that it is a parabolic equation which means you need to use the abc-formula.

I just don't think that there is an easier way than the one I have already presented.


Best Regards,
Richard

Report
Re: About>> b + Sqr(r1² - (x-a)²) = d + Sqr(r2² - (x-c)²) Posted by BitByBit_Thor on 14 Aug 2006 at 1:09 PM
: : Hi,
: :
: : Looks clearer now.
: : I'm gonna try this and let you know.
: : Once it's finish i'll post the app in case others need it.
: :
: : Thanks again
: : Jean
: :
: :
: :
: Hi,
: Yes it's me again
: I tried your formulas and I must be doing something wrong because it doesn't yield the right results.
: I tried with the following values:
: start_x=100
: start_y=100
: end_x=200
: end_y=200
: radius=150
:
: This should put the center at approx. x=56 and y=243 (measured on a paper). The results I get are x=100 and y=1500050.
:
: Here's my code. Do you see anything wrong?
:
: FormuA = 1 + (Start_x - End_x) ^ 2 / (End_y - Start_y) ^ 2
:
: FormuB = 0.5 * (Start_x - End_x) - 0.5 * (Start_x + End_x) * (Start_x - End_x) ^ 2 / (End_y - Start_y) ^ 2
:
: FormuC = 0.25 - Radius ^ 2 + Start_x ^ 2 + Start_y ^ 2 + 0.25 * (Start_y + End_y) ^ 2 + 0.25 * (Start_x + End_x) ^ 2 * (Start_x - End_x) ^ 2 / (End_y - Start_y) ^ 2 - 0.5 * Start_y * (Start_y - End_y)
:

Well one thing I saw is that the last part of the FormuC is Start_y + End_y, instead of minus. No idea if this is the only thing :) The equation if fuzzy enough to make a lot of mistakes.

: FormuD = FormuB ^ 2 - 4 * FormA * FormuC
:
: Center_x1 = (Sqr(FormuD) - FormuB) / (2 * FormuA)
: Center_y1 = ((Start_x - End_x) / (End_y - Start_y)) * Center_x1 + (Start_y + End_y) / 2 - (Start_x - End_x) * (Start_x + End_x) / 2 * (End_y - Start_y)
:
: Thanks for the great help
: JohnBug
:
:

Hmm let me work it out on paper again and see if I made any mistakes...

*here we go* ... *starts to sing*

*half an hour later*

I noticed that they don't look entirely the same as last time, but that could welly be due to choices I made when simplifying.

A = 1 + (x2 - x1) / (y1 - y2)
// Ok so A atleast is the same (they look a bit different, but they aren't)

B = 0.5*(x2 - x1)(y1 + x1 - y2 - x2) / (y1 - y2) + 
  (x1 - x2)y1 / (y1 - y2) - 2x1
// Ok so this one does really look different :-S I have no idea if they
//  are the same...

C = x1 + y1 + 0.25(y1 + x1 - y2 - x2) / (y1 - y2) - 
  0.5y1(y1 + x1 - y2 - x2) / (y1 - y2)
// Well ... B and C have become different looking, but it's really hard
//  to tell if they are actually different or not. Probably, which
//  means I did something wrong the last time or now (or both...)


Well I'd say give them a go :) Be careful to mind all the 's in there.

Best Regards,
Richard

Report
WORKING SOLUTION!John is 1 pixel accuracy good enough?2nd link edited Posted by DrMarten on 15 Aug 2006 at 7:26 AM
This message was edited by DrMarten at 2006-8-15 8:31:27

Hi,

I have working code that proves Richards formulas.

See these screenshots>>
http://i13.photobucket.com/albums/a272/u-might-want-this/Proof1a.jpg

http://i13.photobucket.com/albums/a272/u-might-want-this/Proof2a.jpg

My first point was at 100,100
my second point at 200,200

while the maths and the loop that proved it worked in RADIANS
and not degrees, i could soon change my program to work in degrees.

The code is for VB.Net too but i could change it for standard VB
too if you wish?

This code will work in the FREE DOWNLOAD
of VISUAL BASIC EXPRESS EDITION available from>>
http://msdn.microsoft.com/vstudio/express/vb/


Remember to put this line at the top of your code window
and add one textbox and one button.>>
Imports Microsoft.VisualBasic.ControlChars

John, i have sent you a message which is the entire code.
PASTE it into a completely EMPTY code window
for a fully working program.



Installing the EXPRESS EDITION will not spoil your current
VB setup as it installs to a different folder.



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a, b, f, g, r As Double
        Dim formulaA, formulaB As Double
        Dim formulaC, formulaD As Double
        a = 100 : b = 100 '1st points coordinates.
        f = 200 : g = 200 '2nd points coordinates.
        r = 75 'is the radius value.

        Dim circle1_x, circle1_y As Integer

        formulaA = 1 + (((a - f) ^ 2) / ((g - b) ^ 2))
        'This line uses the line continuation technique type a space and an underscore>> " _"
        formulaB = 0.5 * (a - f) - _
        ((0.5 * (a + f) * ((a - f) ^ 2)) / ((g - b) ^ 2))
        'This line uses the line continuation technique type a space and an underscore>> " _"
        formulaC = 0.25 - _
        (r ^ 2) + _
        (a ^ 2) + _
        (b ^ 2) + _
        (0.25 * ((b + g) ^ 2)) + ( _
        (0.25 * ((a + f) ^ 2) * ((a - f) ^ 2)) / _
        (((g - b) ^ 2) - (0.5 * b * (b + g))))

        formulaD = (formulaB ^ 2) - (4 * formulaA * formulaC)

        circle1_x = CInt(((Math.Sqrt(formulaD)) - formulaB) / (2 * formulaA))
        'This line uses the line continuation technique type a space and an underscore>> " _"
        circle1_y = CInt((((a - f) / (g - b)) * circle1_x) + _
        ((b + g) / 2) - _
        (((a - f) * (a + f)) / (2 * (g - b))))

        'Loop to prove result.>>

        Dim index As Double 'is the loop variable.

        Dim x1, y1 As Double 'are valid plot points that are calculated.

        For index = 0 To (2 * Math.PI) Step 0.01
            x1 = r * Math.Sin(index) + circle1_x 'Uses the calculated cantre X coordinate.
            y1 = r * Math.Cos(index) + circle1_y 'Uses the calculated cantre Y coordinate.
            x1 = CInt(x1) 'Converted to INTEGER.
            y1 = CInt(y1) 'Converted to INTEGER.

            'This line uses the line continuation technique type a space and an underscore>> " _"
            TextBox1.AppendText("x1= " & CStr(x1) & " when y1= " & CStr(y1) & NewLine & _
            "angle= " & CStr(index * (180 / Math.PI)) & " degrees." & NewLine & NewLine)
        Next
    End Sub


Regards,

Dr M.

Report
WORKING code snippet for standard VB, formulas each on 1 line. Posted by DrMarten on 15 Aug 2006 at 8:00 AM
        Dim a, b, f, g, r As Double
        Dim formulaA, formulaB As Double
        Dim formulaC, formulaD As Double
        a = 100 : b = 100 '1st points coordinates.
        f = 200 : g = 200 '2nd points coordinates.
        r = 75 'is the radius value.

        Dim circle1_x, circle1_y As Integer

        formulaA = 1 + (((a - f) ^ 2) / ((g - b) ^ 2))

        formulaB = 0.5 * (a - f) - ((0.5 * (a + f) * ((a - f) ^ 2)) / ((g - b) ^ 2))

        formulaC = 0.25 - (r ^ 2) + (a ^ 2) + (b ^ 2) + (0.25 * ((b + g) ^ 2)) + ((0.25 * ((a + f) ^ 2) * ((a - f) ^ 2)) / (((g - b) ^ 2) - (0.5 * b * (b + g))))

        formulaD = (formulaB ^ 2) - (4 * formulaA * formulaC)

        circle1_x = CInt(((Math.Sqrt(formulaD)) - formulaB) / (2 * formulaA))

        circle1_y = CInt((((a - f) / (g - b)) * circle1_x) + ((b + g) / 2) - (((a - f) * (a + f)) / (2 * (g - b))))



Report
Which formula's, exactly? Posted by BitByBit_Thor on 15 Aug 2006 at 8:15 AM
Just out of curiousity, did you use my first formula's, or the ones I wrote down after working it out on paper again? :P

Best Regards,
Richard

Report
Richard i used the ones in this thread>> Posted by DrMarten on 15 Aug 2006 at 8:24 AM
Hi Richard,

Richard i used the ones in this thread>>

http://www.programmersheaven.com/c/msgboard/read.asp?Board=14&MsgID=343086&Setting=A9999F0001

Regards,

Dr M.


Report
Same VB.Net program written in DEGREES finds exact result. Posted by DrMarten on 16 Aug 2006 at 5:30 AM

Hi all,

This version finds the exact angle for 100,100 and
200,200 being 204 ang 66 degrees respectively.
It is just a coincidence, the program does not by brute
force calculation try to find the angles.



Anyone know why the ouput only reaches 176.75 degrees in the
FOR NEXT loop when step value is changed to 0.25 please?


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a, b, f, g, r As Double
        Dim formulaA, formulaB As Double
        Dim formulaC, formulaD As Double
        a = 100 : b = 100 '1st points coordinates.
        f = 200 : g = 200 '2nd points coordinates.
        r = 75 'is the radius value.

        Dim circle1_x, circle1_y As Integer

        formulaA = 1 + (((a - f) ^ 2) / ((g - b) ^ 2))
        'This line uses the line continuation technique type a space and an underscore>> " _"
        formulaB = 0.5 * (a - f) - _
        ((0.5 * (a + f) * ((a - f) ^ 2)) / ((g - b) ^ 2))
        'This line uses the line continuation technique type a space and an underscore>> " _"
        formulaC = 0.25 - _
        (r ^ 2) + _
        (a ^ 2) + _
        (b ^ 2) + _
        (0.25 * ((b + g) ^ 2)) + ( _
        (0.25 * ((a + f) ^ 2) * ((a - f) ^ 2)) / _
        (((g - b) ^ 2) - (0.5 * b * (b + g))))

        formulaD = (formulaB ^ 2) - (4 * formulaA * formulaC)

        circle1_x = CInt(((Math.Sqrt(formulaD)) - formulaB) / (2 * formulaA))
        'This line uses the line continuation technique type a space and an underscore>> " _"
        circle1_y = CInt((((a - f) / (g - b)) * circle1_x) + _
        ((b + g) / 2) - _
        (((a - f) * (a + f)) / (2 * (g - b))))

        'Loop to prove result.>>

        Dim index As Double 'is the loop variable.

        Dim x1, y1 As Double 'are valid plot points that are calculated.

        For index = 0 To 360 Step 0.5
            x1 = r * Math.Sin(index / (180 / Math.PI)) + circle1_x 'Uses the calculated cantre X coordinate.
            y1 = r * Math.Cos(index / (180 / Math.PI)) + circle1_y 'Uses the calculated cantre Y coordinate.
            x1 = CInt(x1) 'Converted to INTEGER.
            y1 = CInt(y1) 'Converted to INTEGER.

            'This line uses the line continuation technique type a space and an underscore>> " _"
            TextBox1.AppendText("x1= " & CStr(x1) & " when y1= " & CStr(y1) & NewLine & _
            "angle= " & CStr(index) & " degrees." & NewLine & NewLine)
        Next
    End Sub



Regards,

Dr M.

Report
Re: Wow!! Richard ( BitByBit_Thor ), that looked complicated. Posted by Jan_M on 27 Jun 2010 at 8:25 AM
Hi Guys

I'm trying to get this working in C# but not getting correct results.. Does anybody have a working code for C#?

Jan
Report
Re: Wow!! Richard ( BitByBit_Thor ), that looked complicated. Posted by BitByBit_Thor on 14 Jul 2010 at 9:18 AM
Could you post your code (in [code] tags!)?

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
This post has been deleted. Posted by acer243w on 22 Jul 2010 at 11:16 AM
This post has been deleted.
Report
This post has been deleted. Posted by acer243w on 22 Jul 2010 at 11:36 AM
This post has been deleted.
1 2  Next



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.