Mirror

Rotate an ellipse (Views: 713)


Problem/Question/Abstract:

How to draw a rotated Ellipse?

Answer:

I wrote a procedure "CentralRotatedEllipse" to rotate an ellipse. It works exactly enougth for simple graphics. The Ellipse is maked with two connected beziercurves. Rotatingpoint of the Ellipse is its centralpoint. The Parameter canvas for the Destinationcanvas, coordinates  like "common" Ellipse and in alpha the rotatingangle. The function "Rotate2DPoint" you have to put in your code too, its called by the CentralRotatedEllipse-Procedure.
And dont forget uses Math!

function Rotate2DPoint(P, Fix: TPoint; alpha: double): TPoint;
var
  sinus, cosinus: Extended;
begin
  SinCos(alpha, sinus, cosinus);
  P.x := P.x - Fix.x;
  P.y := P.y - Fix.y;
  result.x := Round(p.x * cosinus + p.y * sinus) + fix.x;
  result.y := Round(-p.x * sinus + p.y * cosinus) + Fix.y;
end;

procedure CentralRotatedEllipse(Canvas: TCanvas; x1, y1, x2, y2: Integer; alpha:
  Extended);
var
  PointList: array[0..6] of TPoint;
  f: TPoint;
  dk: Integer;
begin
  dk := Round(0.654 * Abs(y2 - y1));
  f.x := x1 + (x2 - x1) div 2;
  f.y := (y1 + (y2 - y1) div 2) - 1;
  PointList[0] := Rotate2DPoint(Point(x1, f.y), f, Alpha); // Startpoint
  PointList[1] := Rotate2DPoint(Point(x1, f.y - dk), f, Alpha);
  //Controlpoint of Startpoint first part
  PointList[2] := Rotate2DPoint(Point(x2 - 1, f.y - dk), f, Alpha);
  //Controlpoint of secondpoint first part
  PointList[3] := Rotate2DPoint(Point(x2 - 1, f.y), f, Alpha);
  // Firstpoint of secondpart
  PointList[4] := Rotate2DPoint(Point(x2 - 1, f.y + dk), f, Alpha);
  // Controllpoint of secondpart firstpoint
  PointList[5] := Rotate2DPoint(Point(x1, f.y + dk), f, Alpha);
  // Conrollpoint of secondpart endpoint
  PointList[6] := PointList[0]; // Endpoint of
  // Back to the startpoint
  PolyBezier(canvas.handle, Pointlist[0], 7);
end;

Example:

CentralRotatedEllipse(Canvas, 100, 100, 150, 300, DegToRad(45));
CentralRotatedEllipse(Canvas, 100, 100, 150, 300, DegToRad(90));

Angle always should be in Rad.

<< Back to main page