Recent

Author Topic: Library for drawing a path  (Read 961 times)

gidesa

  • Full Member
  • ***
  • Posts: 206
Library for drawing a path
« on: July 07, 2025, 12:43:03 pm »
Hello,
I have a path described as a sequence of (angle difference, distance) couples, generated by a wheel encoder.
The angle difference is relative to previous orientation. The distance is the one travelled
in direction obtained as: previous angle + angle difference.
Is there some simple FPC (or Delphi) library to draw that path on a canvas, for example
a TImage?

Khrys

  • Sr. Member
  • ****
  • Posts: 367
Re: Library for drawing a path
« Reply #1 on: July 07, 2025, 01:26:23 pm »
You could use  TCanvas.Polyline(const Points: array of TPoint);

You'll have to convert your data to an array of points, though - and that could be problematic, because your data points consist of differences.
If you want to find the angle of the  N th line, you'll have to add up the preceding  N - 1  angle differences; in other words, you need to numerically integrate them, and this process is extremely sensitive to errors (especially when using floating-point numbers).

You can try, of course, but don't be surprised when the drawn path rapidly diverges from the actual path when more data points are added.

440bx

  • Hero Member
  • *****
  • Posts: 5909
Re: Library for drawing a path
« Reply #2 on: July 07, 2025, 01:37:02 pm »
It sounds like, if it weren't for the fact that angles are stated in differences from the previous tuple, you'd have an array of polar coordinates.

Following Khrys' idea, I would first convert that array into Cartesian coordinates (x,y) instead of (angle difference, distance).  Note that I'd first calculate the angle by keeping an accumulator to avoid too much error accumulation. 

Once you've got Cartesian coordinates, as Khrys suggested a Polyline will draw your array very quickly.

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12572
  • FPC developer.
Re: Library for drawing a path
« Reply #3 on: July 07, 2025, 01:53:31 pm »
The royal way is to upload it to OpenGL and let a shader do the calculation in the geometry shader.

gues1

  • Guest
Re: Library for drawing a path
« Reply #4 on: July 07, 2025, 01:58:09 pm »
Skia ?

gidesa

  • Full Member
  • ***
  • Posts: 206
Re: Library for drawing a path
« Reply #5 on: July 07, 2025, 03:31:50 pm »
Thank to all.
@Khrys I am aware that this type of calculation diverge quickly. Your suggestion of polyline is good.

@440bx yes, they are in essence cumulative polar coordinates, using an accumulator is very valuable.

Using very rich, and complex, libraries as OpenGL or Skia is a bit too much, in my opinion, for a simple 2D diagram/drawing. 

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12572
  • FPC developer.
Re: Library for drawing a path
« Reply #6 on: July 07, 2025, 04:46:32 pm »
Using very rich, and complex, libraries as OpenGL or Skia is a bit too much, in my opinion, for a simple 2D diagram/drawing.

Depends on border conditions. Opengl often scales better if your element count (number of paths/lines/circles whatever) is high, and if you need to zoom in and out. Zooming can be handled in opengl without reuploading data, making it very fast.    These are the reasons why I created a simple opengl 2D canvas to draw over images.

wp

  • Hero Member
  • *****
  • Posts: 13270
Re: Library for drawing a path
« Reply #7 on: July 07, 2025, 05:53:06 pm »
I have a path described as a sequence of (angle difference, distance) couples, generated by a wheel encoder.
The angle difference is relative to previous orientation. The distance is the one travelled
in direction obtained as: previous angle + angle difference.
Is there some simple FPC (or Delphi) library to draw that path on a canvas, for example
a TImage?
Reminds me of the good-old turtle graphics. Find a simple implementation in the attachment: Create the turtle with its starting position and looking angle and the canvas for drawing, and then make it move by a given distance in a given angle (as seen from the turtle). The sample project contains some restriction of the angle difference - just for fun.

gidesa

  • Full Member
  • ***
  • Posts: 206
Re: Library for drawing a path
« Reply #8 on: July 07, 2025, 07:42:35 pm »
Depends on border conditions. Opengl often scales better if your element count (number of paths/lines/circles whatever) is high, and if you need to zoom in and out. Zooming can be handled in opengl without reuploading data, making it very fast.    These are the reasons why I created a simple opengl 2D canvas to draw over images.

Ah, ok. In my case I have onlu two hundred of data, and no need for zoom.

gidesa

  • Full Member
  • ***
  • Posts: 206
Re: Library for drawing a path
« Reply #9 on: July 07, 2025, 07:43:59 pm »
Reminds me of the good-old turtle graphics. Find a simple implementation in the attachment: Create the turtle with its starting position and looking angle and the canvas for drawing, and then make it move by a given distance in a given angle (as seen from the turtle). The sample project contains some restriction of the angle difference - just for fun.

Yes, just the old good Logo language!  :) Thank you for the example.

 

TinyPortal © 2005-2018