Recent

Author Topic: [SOLVED] Draw line  (Read 2507 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 549
[SOLVED] Draw line
« on: April 27, 2021, 12:53:13 pm »
How can I draw a line from the center of a circle to the edge of the circle at a predefined angle?
« Last Edit: April 27, 2021, 02:34:04 pm by pcurtis »
Windows 10
Laz 2.0.10
FPC 3.2.0

y.ivanov

  • Full Member
  • ***
  • Posts: 110
Re: Draw line
« Reply #1 on: April 27, 2021, 01:49:57 pm »
What is the problem? How to draw a line or what are the start, end coords of the line?
What do you know about the circle?

pcurtis

  • Hero Member
  • *****
  • Posts: 549
Re: Draw line
« Reply #2 on: April 27, 2021, 01:54:15 pm »
The start point is the center of any circle of any radius.
Windows 10
Laz 2.0.10
FPC 3.2.0

winni

  • Hero Member
  • *****
  • Posts: 2356
Re: Draw line
« Reply #3 on: April 27, 2021, 01:55:06 pm »
Schools out forever ....

Anyway:

Code: Pascal  [Select][+][-]
  1. uses .....;math;
  2.  
  3. const deg2rad = pi/180;
  4. var Center, P : TPoint;
  5. angle,sinus,cosinus, radius : single;
  6. ....
  7. angle := 123;
  8. radius := 100;
  9. Center := Point(150,150);
  10. sincos(angle*deg2rad,sinus,cosinus);
  11. P := Point(round(radius*cosinus)+Center.x, round(radius*sinus)+Center.y);
  12. canvas.Line(center,P);
  13.  

Angle starts in Lazarus in East!

Winni

y.ivanov

  • Full Member
  • ***
  • Posts: 110
Re: Draw line
« Reply #4 on: April 27, 2021, 01:56:07 pm »
The start point is the center of any circle of any radius.

Correct! But that is an answer to different question. ;)
 

pcurtis

  • Hero Member
  • *****
  • Posts: 549
Re: Draw line
« Reply #5 on: April 27, 2021, 02:33:13 pm »
@winni - thanks
Windows 10
Laz 2.0.10
FPC 3.2.0

y.ivanov

  • Full Member
  • ***
  • Posts: 110
Re: [SOLVED] Draw line
« Reply #6 on: April 27, 2021, 03:22:12 pm »
@winni, you're the man. You can guess whats the OP's actual question is.

BTW, the angle can be degs, rads, or x/y
the circle can be center+radius, bounding box, line segment,
It depends on the task

furious programming

  • Hero Member
  • *****
  • Posts: 505
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: [SOLVED] Draw line
« Reply #7 on: May 03, 2021, 12:31:50 am »
@y.ivanov: See this gif for how the sin and cos functions are used for such calculations — https://upload.wikimedia.org/wikipedia/commons/3/3b/Circle_cos_sin.gif

All you need to do is have the coordinates of the starting point (this is the center of the circle), the length of the line and the angle at which to paint. The angle must be expressed in radians, so if you have it in degrees, use the DegToRad and RadToDeg functions — details can be found in the documentation.

So, having a start point, the end point calculation looks like this:

Code: Pascal  [Select][+][-]
  1. DestX := SourceX + Cos(Angle) * Radius;
  2. DestY := SourceY + Sin(Angle) * Radius;

This gives you the coordinates of two points that you can use to draw the line, for example with Canvas.Line.
Lazarus 2.0.12 with FPC 3.2.0 (SVN Revision 64642), Windows 10 — all 64-bit

y.ivanov

  • Full Member
  • ***
  • Posts: 110
Re: [SOLVED] Draw line
« Reply #8 on: May 03, 2021, 01:30:24 am »
@furious programming
I appreciate a lot your concern and explanation,

My wrong, I just overestimated the original question a little bit. FYI, I have strong math background and wanted to give simpler answer to OP.  It is not always necessary to use trigonometric functions to calculate simple tasks involving circles.

But you probably know that ;)

For example, in an interactive program, instead of angle in degrees, you may have a slope given by Δx, Δy (mouse movement) which is almost an equivalent to angle in radians. Then you can use even the integer math. 

furious programming

  • Hero Member
  • *****
  • Posts: 505
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: [SOLVED] Draw line
« Reply #9 on: May 04, 2021, 01:26:43 am »
I thought you were the OP, I pinged the wrong person. Apologize.

FYI, I have strong math background and wanted to give simpler answer to OP.

The simplest answer is just to use Sin and Cos, because it is actually simple and widely used.

Quote
It is not always necessary to use trigonometric functions to calculate simple tasks involving circles.

Yes, if you are writing code for a processor that does not support floating point arithmetic. Otherwise, such weirdness does not make sense and instead of making it easier and faster to solve a simple task, it only complicates it. But you probably know that.
Lazarus 2.0.12 with FPC 3.2.0 (SVN Revision 64642), Windows 10 — all 64-bit

y.ivanov

  • Full Member
  • ***
  • Posts: 110
Re: [SOLVED] Draw line
« Reply #10 on: May 04, 2021, 01:49:47 am »
I thought you were the OP, I pinged the wrong person. Apologize.

Apologies accepted.
Pity, the complication is one of the programming sins in which I'm involved :(

 

TinyPortal © 2005-2018