Port Delphi Canvas Function

Recommended Posts

How to port Delphi Canvas function to SMS?

Ellipse(X1,Y1,X2,Y2);

Example:

Ellipse(x-r,y-r,x+r,y+r);

Thanks

Shane

Share on other sites

You can do an ellipse by applying scale to an arc, f.i.:

```
Canvas.Scale(1, 0.5);
Canvas.BeginPath;
Canvas.ArcF(200, 210, 100, 0, PI*2, false);
Canvas.Scale(1, 2);
Canvas.Stroke;
```

The second scale call restores the previous scale, and has to be done before the Stroke, otherwise the scale will also get applied to the stroke thickness.

Arc takes center coordinates followed by radius, so the scale ratio would be Abs((Y2-Y1)/(X2-X1)) if I'm not mistaken (and its inverse for the second scale call)

Share on other sites

I've added Ellipse to TW3Canvas for the next release, code looks like:

```
procedure TW3Canvas.Ellipse(const aRect : TRectF);
begin
var dx := aRect.Width;
var dy := aRect.Height;
var f := if dy<>0 then dx/dy else 1;
FDC.beginPath();
FDC.scale(f, 1);
FDC.arc((aRect.Right+aRect.Left)*0.5, (aRect.Top+aRect.Bottom)*0.5,
dy*0.5, 0, 2*PI, false);
FDC.scale(1/f, 1);
FDC.stroke();
end;
```

That's starting from the bounding rect, if you start from center + x/y radius, things can be further simplified.

Share on other sites

I actually started on a TCanvas wrapper last week to help emulate the old VCL. It will just consume the standard canvas (e.g: FMyCanvas:=TVCLCanvas.Create(Canvas) ). Also thinking about porting over some other canvas types, like CODEF - which is really cool

Create an account

Register a new account