Question

I am using Lazarus because I really like the TChart components. I have made a program that solves some inequalities like:

http://img692.imageshack.us/img692/5659/esl6.png

(Delta = Discriminant) The solutions for 2x^2+6x+4>0 are x<-2 and x>-1. This is correct but as you can see I have a TPicture component below that shows a picture with a chart. I made that with Gimp.

I'd like to make a chart using some apposite components. I found them on Lazarus as I have already said, but do you know if there are any ways on Delphi XE5 too? Where could I take a look at?

Was it helpful?

Solution

First, your set notation is very strange. You mean S = (−∞, −2) ∪ (−1, ∞), that is, S is the union of two open intervals. You can also write S = [−2, −1]^C or S = {x ∈ ℝ: x < −2 or x > −1}. Also, your picture is certainly not a parabola; it looks much more like a hyperbola.

Anyhow, plotting the graphs of functions f: ℝ → ℝ is simple. You only need to take care of the coordinate transformation between your logical coordinate system and the screen's coordinate system. Define

type
  TRealVector = record
    X, Y: real;
  end;

as a point in ℝ² and your maps are

const
  xmin = -10;
  xmax = 10;
  ymin = -10;
  ymax = 10;

function TForm5.LogToScreen(LogPoint: TRealVector): TPoint;
begin
  result.X := round(ClientWidth * (LogPoint.X - xmin) / (xmax - xmin));
  result.Y := ClientHeight - round(ClientHeight * (LogPoint.Y - ymin) / (ymax - ymin));
end;

function TForm5.ScreenToLog(ScreenPoint: TPoint): TRealVector;
begin
  result.X := xmin + (ScreenPoint.X / ClientWidth) * (xmax - xmin);
  result.Y := ymin + (ymax - ymin) * (ClientHeight - ScreenPoint.Y) / ClientHeight;
end;

Then you just have to plot!

procedure TForm5.FormPaint(Sender: TObject);
var
  PrevPoint, CurrPoint: TPoint;
  x: integer;
  logx: real;
  logy: real;
  y: integer;
begin
  PrevPoint := Point(-1, -1);
  Canvas.Brush.Color := clWhite;
  Canvas.FillRect(ClientRect);
  for x := 0 to ClientWidth - 1 do
  begin
    logx := ScreenToLog(Point(x, 0)).X;
    logy := logx*logx; // y = f(x)
    y := LogToScreen(RealVector(logx, logy)).Y;
    CurrPoint := Point(x, y);
    if PrevPoint.X = -1 then
      Canvas.MoveTo(CurrPoint.X, CurrPoint.Y)
    else
      Canvas.LineTo(CurrPoint.X, CurrPoint.Y);
    PrevPoint := CurrPoint;
  end;
end;

Don't forget:

procedure TForm5.FormResize(Sender: TObject);
begin
  Invalidate;
end;

Screenshot
(source: rejbrand.se)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top