# Board index » delphi » D1: Drawing line from circle

## D1: Drawing line from circle

Want to draw a line of a certain length from a point on a circle radiating from
the center point ( either towards or away from center ).
Know the radius (R), know the center point (X1,Y1), where on the circle to draw
the line (X2, Y2), length of the line (L), and towards/away from center point
(true/false). But no idea how to do it.

Any help appreciated
Chris
c8...@aol.com

## Re:D1: Drawing line from circle

##### Quote
C8591 wrote:

> Want to draw a line of a certain length from a point on a circle radiating from
> the center point ( either towards or away from center ).
> Know the radius (R), know the center point (X1,Y1), where on the circle to draw
> the line (X2, Y2), length of the line (L), and towards/away from center point
> (true/false). But no idea how to do it.

OK. Let's say we're going to draw the line from (X2, Y2) to (X,Y).
(So we MoveTo(X2,Y2) and then LineTo(X,Y), and the question is how to figure
out what X and Y are).

First the "outwards" case: The vector (X,Y) - (X2,Y2) should be
parallel to (X2,Y2) - (X1,Y1), and in the same direction. This means that

(1)     (X,Y) - (X2,Y2) = c*((X2,Y2) - (X1,Y1))

for some number c > 0 . If we can figure out what c should be we're
in business, we solve (1) for X and Y and there we are. Look at the
lengths: the length of the vector on the left side of (1) is L, and the
length of the vector on the right is

c*sqrt((X2-X1)^2 + (Y2-Y1)^2).

So c = L / sqrt((X2-X1)^2 + (Y2-Y1)^2) ; you plug this into (1) and you
get

X = X2 + c*(X2 - X1);
Y = Y2 + c*(Y2 - Y1);

(The thing to do is do all the calculations with floating-point types
and round to integers at the last minute, just before actually drawing
the line.)

Drawing the line pointing into the circle is the same, except
those twpo vectors should be parallel in _opposite_ directions, so
you want c < 0 . So exactly the same formulas work except
c = - L / sqrt((X2-X1)^2 + (Y2-Y1)^2) .

--
David Ullrich

## Re:D1: Drawing line from circle

In article <34E889F4.6...@math.okstate.edu>, David Ullrich

##### Quote
<ullr...@math.okstate.edu> writes:
>(1)         (X,Y) - (X2,Y2) = c*((X2,Y2) - (X1,Y1))

>for some number c > 0 . If we can figure out what c should be we're
>in business, we solve (1) for X and Y and there we are. Look at the
>lengths: the length of the vector on the left side of (1) is L, and the
>length of the vector on the right is

>c*sqrt((X2-X1)^2 + (Y2-Y1)^2).

>So c = L / sqrt((X2-X1)^2 + (Y2-Y1)^2) ; you plug this into (1) and you
>get

>X = X2 + c*(X2 - X1);
>Y = Y2 + c*(Y2 - Y1);

>(The thing to do is do all the calculations with floating-point types
>and round to integers at the last minute, just before actually drawing
>the line.)

Surely (I hate saying this again David <g> & it sounds like I'm being
smartarse)
by inspection C = L/R, because X = X2 + L/R*(X2 - X1);

X1---------(R)----------X2------------(L)-----------X

similarly for Y.

Having said that, I've just worked out that  sqrt((X2-X1)^2 + (Y2-Y1)^2) = R so
algebraically it does simplify to L/R.

Alan Lloyd
alangll...@aol.com

q

## Re:D1: Drawing line from circle

The following routine can be used to a plot a new
pixel location at a given distance (in pixels) and bearing
in degrees.
This may be helpful in developing your solution.

Procedure PixLoc(x,y : LongInt; Var x1, y1 : LongInt; distance,bearing :
Double);
// Plot new pixel location at Bearing and Distance from x,y.
// x1,y1 is the new location at bearing and distance in pixels
Begin
x1 := x + Round(Sin(DegToRad(Bearing)) * Distance);
y1 := y - Round(Cos(DegToRad(Bearing)) * Distance);
End;  // PicLoc

Bill

C8591 <c8...@aol.com> wrote in article

##### Quote
> Want to draw a line of a certain length from a point on a circle
> the center point ( either towards or away from center ).
> Know the radius (R), know the center point (X1,Y1), where on the circle
to draw
> the line (X2, Y2), length of the line (L), and towards/away from center
point
> (true/false). But no idea how to do it.

> Any help appreciated
> Chris
> c8...@aol.com

## Re:D1: Drawing line from circle

##### Quote
AlanGLLoyd wrote:

> In article <34E889F4.6...@math.okstate.edu>, David Ullrich
> <ullr...@math.okstate.edu> writes:

> >(1)    (X,Y) - (X2,Y2) = c*((X2,Y2) - (X1,Y1))

> >for some number c > 0 . If we can figure out what c should be we're
> >in business, we solve (1) for X and Y and there we are. Look at the
> >lengths: the length of the vector on the left side of (1) is L, and the
> >length of the vector on the right is

> >c*sqrt((X2-X1)^2 + (Y2-Y1)^2).

> >So c = L / sqrt((X2-X1)^2 + (Y2-Y1)^2) ; you plug this into (1) and you
> >get

> >X = X2 + c*(X2 - X1);
> >Y = Y2 + c*(Y2 - Y1);

> >(The thing to do is do all the calculations with floating-point types
> >and round to integers at the last minute, just before actually drawing
> >the line.)

> Surely (I hate saying this again David <g> & it sounds like I'm being
> smartarse)
> by inspection C = L/R, because X = X2 + L/R*(X2 - X1);

>   X1---------(R)----------X2------------(L)-----------X

> similarly for Y.

> Having said that, I've just worked out that  sqrt((X2-X1)^2 + (Y2-Y1)^2) = R so
> algebraically it does simplify to L/R.

Well, first you're assuming that "R" always means "the radius of the
circle" - if we have a circle of radius 1 and another circle of radius 2 in
the same problem it follows that 1 = 2. But that's just being pedantic about
terminology, so never mind. Seriously:

Of _course_ c is L / R ! I didn't say it wasn't, did I? We were not
_given_ R in the original problem, all we were given was (X1,Y1) and (X2,Y2),
so I assumed that we wanted a solution in terms of (X1,Y1) and (X2,Y2).

(Where did you think that sqrt((X2-X1)^2 + (Y2-Y1)^2) thing came from
anyway? The fact that sqrt((X2-X1)^2 + (Y2-Y1)^2) is the radius of the
circle wasn't supposed to be a big secret, I thought that was obvious. If
we're given what we're given do you know a way to find R other than
saying R = sqrt((X2-X1)^2 + (Y2-Y1)^2) ???)

--
David Ullrich

## Re:D1: Drawing line from circle

In article <34E9DA5F.5...@math.okstate.edu>, David Ullrich

##### Quote
<ullr...@math.okstate.edu> writes:
>? We were not
>_given_ R in the original problem, all we were given was (X1,Y1) and (X2,Y2),
>so I assumed that we wanted a solution in terms of (X1,Y1) and (X2,Y2).

From original posting :-

In article <19980216154001.KAA28...@ladder02.news.aol.com>, c8...@aol.com

##### Quote
(C8591) writes:
>Know the radius (R), know the center point (X1,Y1), where on the circle to
>draw the line (X2, Y2), length of the line (L), and towards/away from
>center point (true/false).

He said "the circle" ie one circle.

I'd just rather calculate the easiest way, and I'd certainly rather not
calculate square roots unless I _have_ to !

Alan Lloyd
alangll...@aol.com

## Re:D1: Drawing line from circle

##### Quote
AlanGLLoyd wrote:
> [...]
> >Know the radius (R),[...]

Sorry.

--
David Ullrich

## Re:D1: Drawing line from circle

##### Quote
>> Want to draw a line of a certain length from a point on a circle
>> the center point ( either towards or away from center ).

Thanks for the help, works just fine.

btw: tried " sqrt((X2-X1)^2 + (Y2-Y1)^2) " but got error #121 (Invalid
Qualifier) cause of " ^2 " . ???

Chris
c8591@~despammer~aol.com

## Re:D1: Drawing line from circle

##### Quote
C8591 wrote:

> >> Want to draw a line of a certain length from a point on a circle
> >> the center point ( either towards or away from center ).

> Thanks for the help, works just fine.

> btw: tried " sqrt((X2-X1)^2 + (Y2-Y1)^2) " but got error #121 (Invalid
> Qualifier) cause of " ^2 " . ???

None of what I wrote was actual Delphi code (if some of it
actually compiled that was by accident). The "^2" meant "squared" -
this is sort of standard usage. (Like you really thought an
explanation of a problem in high-school geometry was going to
involve dereferencing a pointer?)

If you have math.pas you can probably figure out how to
square a number...

--
David Ullrich

## Re:D1: Drawing line from circle

David Ullrich schrieb in Nachricht <34F071C6.5...@math.okstate.edu>...

##### Quote
>C8591 wrote:

>> btw: tried " sqrt((X2-X1)^2 + (Y2-Y1)^2) " but got error #121
(Invalid
>> Qualifier) cause of " ^2 " . ???

> None of what I wrote was actual Delphi code (if some of it
>actually compiled that was by accident). The "^2" meant "squared" -
>this is sort of standard usage.... [snip]

Perhaps you have already forgotten, but afaik the syntax ^2 comes from
(sorry, but I'll have to use the word) BASIC. I know this is hard to
accept for Delphians (like me). ;))

Pascal uses Sqr(X: Extended): Extended.

Rudy Velthuis

I couldn't find your sig.txt either ;)

## Re:D1: Drawing line from circle

##### Quote
>> btw: tried " sqrt((X2-X1)^2 + (Y2-Y1)^2) " but got error #121 (Invalid
>> Qualifier) cause of " ^2 " . ???
David Ullrich wrote:

>    None of what I wrote was actual Delphi code (if some of it
>actually compiled that was by accident). The "^2" meant "squared" -
>this is sort of standard usage. (Like you really thought an
>explanation of a problem in high-school geometry was going to
>involve dereferencing a pointer?)
>...

No. It actually didn't occur to me that it wasn't Delphi code. As far as "^2"
meaning squared, I never knew that, learn something all the time.

Chris
c8591@~despammer~aol.com

## Re:D1: Drawing line from circle

##### Quote
Rudy Velthuis wrote:

> David Ullrich schrieb in Nachricht <34F071C6.5...@math.okstate.edu>...
> >C8591 wrote:

> >> btw: tried " sqrt((X2-X1)^2 + (Y2-Y1)^2) " but got error #121
> (Invalid
> >> Qualifier) cause of " ^2 " . ???

> > None of what I wrote was actual Delphi code (if some of it
> >actually compiled that was by accident). The "^2" meant "squared" -
> >this is sort of standard usage.... [snip]

> Perhaps you have already forgotten, but afaik the syntax ^2 comes from
> (sorry, but I'll have to use the word) BASIC. I know this is hard to
> accept for Delphians (like me). ;))

> Pascal uses Sqr(X: Extended): Extended.

Actually I knew this. I went back and looked: in fact there was
_no_ _code_ _at_ _all_ in what I wrote, just a bunch of mathematical expressions.
(And x^y is extremely standard on-line notation for "x to the y", no doubt
because of BASIC, (Fortran?), TEX, etc.)

--
David Ullrich