Answer the question
In order to leave comments, you need to log in
How to remove the line connecting (0,0) and the beginning of ln(x) Tcanvas graph?
Good day!
I ran into a problem when I started to master plotting in delphi. The fact is that I wanted to try to plot ln (x) with a canvas, but ln has a restriction x> 0, and Delphi draws a line from the origin in image to the same value in x> 0. How to get rid of it and leave only ln'a chart??
Here is the code
procedure TForm1.Button5Click(Sender: TObject);
const mash=10;
Var x0,y0:integer;
x,y:real;
begin
x0:=image2.Width div 2;
y0:=image2.Height div 2;
// рисуем ось
image2.canvas.pen.color:=clgray;
image2.canvas.pen.width:=2;
image2.Canvas.MoveTo(x0,0);
image2.Canvas.LineTo(x0,clientheight);
image2.canvas.moveto(0,y0);
image2.Canvas.lineto(clientwidth,y0);
//рисуем график
image2.Canvas.Pen.Color:=clred;
x:=0;
image2.canvas.moveto(trunc(x),trunc(y));
repeat
x:=x+0.001;
if x <= 0 then
y := 0
else
y:=-ln(x);
image2.canvas.lineto(trunc(x*mash)+x0,trunc(y*mash)+y0);
until x>=50;
end;
Answer the question
In order to leave comments, you need to log in
Direct reasons for this segment.
1. In the first MoveTo you confused real and screen coordinates. And in the first graph, apparently, too, only there the line goes over the edge to the left.
2. You do not have any logic of behavior on breaks. In the simplest case - to the first point MoveTo, not LineTo.
And other mistakes.
3. Not initialized y.
4. You're painting on the wrong canvas. Apparently, you wanted to buffer the graphs - but the graphs are not printed on the picture, but are drawn on the form canvas. As soon as you minimize-restore the program, the graphs will disappear. It is necessary to draw either in the OnPaint event (then the graph will be rebuilt every time it is redrawn), or printed on Picture.Bitmap (the graph will remain printed on the picture). In the second case, don't forget image2.Invalidate (deferred redraw when possible);
5. Step too small. It should be the equivalent of one or two pixels.
RELATED NOTE 4. On WinVista+ with Aero enabled, you might be fine (Aero buffers windows to allow Aero Flip and stuff). Turn off Aero, and there will be the usual "hardcore" Windows: it does not remember anything, and when necessary, it redraws.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question