I have already seen this example and I've read/seen a lot of documentation/video but I am stucked at a particular point. I have used this code to draw a parabola in a PlotGrid
.
procedure TForm1.PlotGridPaint(Sender: TObject; Canvas: TCanvas;
const [Ref] ARect: TRectF);
var i: integer;
x: double;
FPoints: TPolygon;
xPixels, yPixels: double;
Origin: TPointF;
begin
SetLength(FPoints, 600);
PlotGrid.Canvas.Stroke.Color := TAlphaColorRec.Darkred;
PlotGrid.Canvas.Stroke.Thickness := 2;
//sizes
Origin.X := PlotGrid.Width/2;
Origin.Y := PlotGrid.Height/2;
x := -300;
for I := 0 to High(FPoints) do
begin
FPoints[I].X := Origin.x + x;
FPoints[I].Y := Origin.y - (x*x); //plot f(x) = x^2
x := x + 1;
end;
for i := 1 to High(FPoints) do
begin
Canvas.DrawLine(FPoints[i-1], FPoints[i], 1);
end;
end;
This code works and it produces this output.
As you can see it is a (weird) function x^2. Of course this line of code represents the equation:
FPoints[I].Y := Origin.y - (x*x); //plot f(x) = x^2
If I changed the code above with Origin.y - (2*x*x - 5)
it means that I am going to plot f(x) = 2x^2 - 5. I want to point out that:
The code works fine because it draws the correct parabola but the result is not optimal because there is a lack of resolution. I would like to be able to "zoom in" the parabola and I have tried to manipulate the X coords, but I didn't have good results.
Do you have any idea? How could I zoom the parabola and change the resolution according with the screen size?
I was thinking to do this but it seems to be the wrong way:
for I := 0 to High(FPoints) do
begin
FPoints[I].X := Origin.x + x * 30;
FPoints[I].Y := Origin.y - (-x*x) * 30;
x := x + 1;
end;
The result is this:
It is better but the curve is not "smooth" and I have added a multiplier of 30. I am not sure if it works fine in a mobile device as well. Any suggestion?
When you multiplied the point coordinates you did indeed zoom in on the parabola bottom, but made the image coarser, because you reduced the frequency of the points. You actually want to do the opposite, zoom in but keep (or increase) point frequency. You obviously also selected the range of x values thinking about pixels on the screen, but with a x^2 function the y values goes out of screen all too soon, and you need to scale the y values.
I suggest you retain the x values as now and scale with the y values
FPoints[i].Y := Origin.Y - (x * x * 0.1); // plot f(x) = x^2 * 0.1
I made a test adding another TPolygon
and giving it the y values
FPoints2[i].Y := Origin.Y - (x * x * 0.01); // plot f(x) = x^2 * 0.01
The second series is painted wih dark blue and the result is: