Mega Code Archive

 
Categories / Delphi / VCL
 

How do i plot a line

Question: How do I plot a line? Answer: The easiest way to step along each point in a line segment is to call the Windows API function LineDDA(). LineDDA() takes the x and y start and end points of the line you wish to plot, and the address of a function in your program, and calls this function for each pixel that needs to be drawn. LineDDA() accepts an additional parameter "LParam" that gets passed back to your function each time it is called. The value of LParam can be anything you wish, and is useful for passing handles and pointers. Some typecasting of this parameter may be necessary to achieve the functionality you desire. The following example shows how to plot a line from 10,10 to 100,100, and draw a circle along the line if the x coordinate of the pixel position is an even multiple of 10. Example: procedure DDAProc(x : integer; y : integer; LParam : LongInt) {$IFDEF WIN32} stdcall; {$ELSE} ; export; {$ENDIF} begin if x mod 10 = 0 then TCanvas(LParam).Ellipse(x - 10, y - 10, x + 10, y + 10); end; procedure TForm1.Button1Click(Sender: TObject); begin LineDDA(10, 10, 100, 100, @DDAProc, {$IFDEF WIN32} LongInt(Form1.Canvas) {$ELSE} Form1.Canvas {$ENDIF} ); end;