Recent

Author Topic: Angled Ellipse  (Read 595 times)

Badger

  • Full Member
  • ***
  • Posts: 158
Angled Ellipse
« on: October 08, 2025, 07:34:59 am »
I don't think this is possible but I live in hope.
I want to draw an ellipse on a canvas at an angle.  This would require 4 points in stead of two. is this possible?
Badger
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v3.6  FPC 3.2.2   Win 32/64

Khrys

  • Sr. Member
  • ****
  • Posts: 348
Re: Angled Ellipse
« Reply #1 on: October 08, 2025, 07:56:39 am »
If you're targeting Windows only, you could take a look at  SetWorldTransform

wp

  • Hero Member
  • *****
  • Posts: 13219
Re: Angled Ellipse
« Reply #2 on: October 08, 2025, 11:13:30 am »
Which 4 points exactly?

Basically you need the lengths of both (unrotated) half-axes, the center point as well as the rotation angle. Then you can calculate points on the ellipse perimeter and connect/fill them by Canvas.PolyLine/.Polygon (or by Canvas.PolyBezier, but this requires more work to find out the Bezier control points):

Code: Pascal  [Select][+][-]
  1. procedure TForm1.PaintBox1Paint(Sender: TObject);
  2. var
  3.   i, N: Integer;
  4.   phi: Single;
  5.   sinPhi, cosPhi, alpha: Single;
  6.   P: array of TPoint = nil;
  7.   C: TPoint;
  8. begin
  9.   N := 36;
  10.   SetLength(P, N+1);
  11.   C := Point(Paintbox1.Width div 2, Paintbox1.Height div 2);
  12.   alpha := pi/180 * ScrollBar1.Position;
  13.   for i := 0 to N do
  14.   begin
  15.     phi := 2.0*pi*i / N;
  16.     SinCos(phi, sinPhi, cosPhi);
  17.     P[i] := RotatePoint(Point(round(A*cosPhi), round(B*sinPhi)), alpha) + C;
  18.   end;
  19.   Paintbox1.Canvas.Brush.Color := clYellow;
  20.   Paintbox1.Canvas.Polygon(P);
  21. end;
  22.  
  23. procedure TForm1.FormCreate(Sender: TObject);
  24. begin
  25.   A := (Paintbox1.Width - 40) div 2;
  26.   B := (Paintbox1.Height - 40) div 2;
  27. end;
  28.  
  29. procedure TForm1.ScrollBar1Change(Sender: TObject);
  30. begin
  31.   Paintbox1.Invalidate;
  32. end;                              

If you look for more ellipse math, look at the code in fpvectorial.pas which you can find the folder components/fpvectorial of your Lazarus installation.

Badger

  • Full Member
  • ***
  • Posts: 158
Re: Angled Ellipse
« Reply #3 on: October 08, 2025, 01:23:34 pm »
Quote
Which 4 points exactly?
When you draw an elipse at 90 degrees, you only pass 2 points - Canvas.Ellipse(Pointa,pointb).   This provides info on the surrounding rectangle sides - top, left, right and bottom.
If it were possible to draw an angled ellipse using a similar process, you would need to take into consideration all 4 corner points of the surrounding box to account for the angle.
 I have been approximating the polygon method WP proposes by solving for an octigon (8 points) but thought if there was an ellipse solution it would look neater.  However, because this is a simulation with multiple elipses and considerable maths to locate them, I think a 36 point rendering for each would slow things down too much.
Thank you both for you input.
« Last Edit: October 08, 2025, 01:35:10 pm by Badger »
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v3.6  FPC 3.2.2   Win 32/64

MathMan

  • Sr. Member
  • ****
  • Posts: 434
Re: Angled Ellipse
« Reply #4 on: October 08, 2025, 02:13:56 pm »
Hello Badger,

Don't know if you already have, but maybe take a look at Bresenham's algorithm - which also exists for angled ellipses.

Cheers,
MathMan

Gigatron

  • Sr. Member
  • ****
  • Posts: 334
  • Amiga Rulez !!
Re: Angled Ellipse
« Reply #5 on: October 08, 2025, 03:22:17 pm »
Trip to Europe...  20 days

wp

  • Hero Member
  • *****
  • Posts: 13219
Re: Angled Ellipse
« Reply #6 on: October 08, 2025, 04:00:27 pm »
You mean the corner points of the rotated rectangle enclosing the ellipse? Yes - this should work. You only must determine the ellipse parameters from these points:
  • Calculate the center of the rotated rectangle to get the center C of the ellipse (mean value of the 4 point coordinates)
  • Calculate the lengths of the long and short sides of the rectangle. Multiplied by 0.5 you get the ellipse half axes A and B, respectively.
  • Calculate the angle of the long side to the x axis - this is the rotation angle
  • you can put these numbers into the code that I posted above to draw the rotated ellipse

 

TinyPortal © 2005-2018