Solved

Start-Endpoint

Posted on 2001-06-23
13
303 Views
Last Modified: 2010-04-06
Hello. Ok, I know the startpoint (0,0) and the endpoint (233,417). So first:
Canvas.Moveto(0,0);
Canvas.Lineto(233,417);
Now I need to know all the different points between (0,0) and (233,417). I want to put them in a array.
Any suggestions?
0
Comment
Question by:zerotwo
13 Comments
 
LVL 3

Expert Comment

by:f15iaf
ID: 6221709
you have the canvas.pixels[x,y] array and if canvas is in bitmap it's even better because you have scanline property which you can copy directly to array
0
 
LVL 6

Assisted Solution

by:zebada
zebada earned 50 total points
ID: 6221871
Take a look at (winAPI) LineDDA() function - it calls back each time it draws a pixel and you can then save the pixel's x,y location on each call back.
I've not used it but I'm sure it does what you want.
0
 
LVL 4

Accepted Solution

by:
DMN earned 50 total points
ID: 6221950
Zebada is right. Here is a sample:

{===================== PAS =====================}
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure MyLineDDA(x,y:integer;lparam:TStrings); stdcall;
begin
  lparam.add(inttostr(x)+','+inttostr(y));
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  LineDDA(0,0,100,175,@MyLineDDA,integer(Memo1.Lines));
end;

end.

{===================== DFM =====================}
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 544
  Height = 375
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 20
    Top = 16
    Width = 261
    Height = 269
    ScrollBars = ssVertical
    TabOrder = 0
  end
end

0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6222467
zerotwo, if you want to computer the points of a line yourself (that is, without the help of Windows and the callback), you can do it with a floating point routine (easier to understand) or using the bresenham algorithm (the standard for line drawing, using only integers).

Link for bresenham explanation:
http://www.gamedev.net/reference/articles/article767.asp
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6222921
That is the Pascal source i found for Bresenham.
Quite old isn't it? Works perfect.
I use it to smooth a mouse movement.

{BEGIN:  BRESENHM.TXT       8 30-May-88    48   512  Textfile }
PROGRAM Bresenham;

  PROCEDURE DrawPixel (x,y:  INTEGER);
  BEGIN
    WRITELN ('(x,y) = (',x:3,',',y:3,')')
  END {DrawPixel};

  PROCEDURE DrawLine (xStart,yStart, xEnd,yEnd:  INTEGER);
  {Bresenham's Line Algorithm.  Byte, March 1988, pp. 249-253.}
    VAR
      a,b        :  INTEGER;  {displacements in x and y}
      d          :  INTEGER;  {decision variable}
      diag_inc   :  INTEGER;  {d's increment for diagonal steps}
      dx_diag    :  INTEGER;  {diagonal x step for next pixel}
      dx_nondiag :  INTEGER;  {nondiagonal x step for next pixel}
      dy_diag    :  INTEGER;  {diagonal y step for next pixel}
      dy_nondiag :  INTEGER;  {nondiagonal y step for next pixel}
      i          :  INTEGER;  {loop index}
      nondiag_inc:  INTEGER;  {d's increment for nondiagonal steps}
      swap       :  INTEGER;  {temporary variable for swap}
      x,y        :  INTEGER;  {current x and y coordinates}
  BEGIN {DrawLine}
    x := xStart;              {line starting point}
    y := yStart;
    {Determine drawing direction and step to the next pixel.}
    a := xEnd - xStart;       {difference in x dimension}
    b := yEnd - yStart;       {difference in y dimension}
    {Determine whether end point lies to right or left of start point.}
    IF   a < 0                {drawing towards smaller x values?}
    THEN BEGIN
      a := -a;                {make 'a' positive}
      dx_diag := -1
    END
    ELSE
      dx_diag := 1;
    {Determine whether end point lies above or below start point.}
    IF   b < 0                {drawing towards smaller x values?}
    THEN BEGIN
      b := -b;                {make 'a' positive}
      dy_diag := -1
    END
    ELSE
      dy_diag := 1;
    {Identify octant containing end point.}
    IF   a < b
    THEN BEGIN
      swap := a;
      a := b;
      b := swap;
      dx_nondiag := 0;
      dy_nondiag := dy_diag
    END
    ELSE BEGIN
      dx_nondiag := dx_diag;
      dy_nondiag := 0
    END;
    d := b + b - a;            {initial value for d is 2*b - a}
    nondiag_inc := b + b;      {set initial d increment values}
    diag_inc    := b + b - a - a;
    FOR i := 0 TO a DO BEGIN   {draw the a+1 pixels}
      DrawPixel (x,y);
      IF   d < 0               {is midpoint above the line?}
      THEN BEGIN               {step nondiagonally}
        x := x + dx_nondiag;
        y := y + dy_nondiag;
        d := d + nondiag_inc   {update decision variable}
      END
      ELSE BEGIN               {midpoint is above the line; step diagonally}
        x := x + dx_diag;
        y := y + dy_diag;
        d := d + diag_inc
      END
    END;
    WRITELN;
    READLN
  END {DrawLine};

BEGIN
  DrawLine (0,0, 10,  4);
  DrawLine (0,0,  4, 10);
  DrawLine (0,0, -4, 10);
  DrawLine (0,0,-10,  4);
  DrawLine (0,0,-10, -4);
  DrawLine (0,0, -4,-10);
  DrawLine (0,0,  4,-10);
  DrawLine (0,0, 10, -4)
END {Bresenham}.

{END:  GRAPH:BRESENHAM.TEXT  }
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6224388
robert_marquart, the age of an algorithm does not make it fall apart ;-)

Anyways, the URL I posted does include a full, well-explained Pascal source sample also. It may be easier to understand due to the comments and explanations than the one posted above.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:zerotwo
ID: 6228856
Well,I agree with AvonWyss, but maybe I can use it still...
By the way DMN, if you could document your sample code, so that even I can understand it, you would deserve the points... but first I have to check out if it works.
Maybe we need to split points!?!?!
0
 

Author Comment

by:zerotwo
ID: 6228862
Is BRESENHAM some sort of a Delpi/Pascal supercoder??
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6229262
Bresenham is a smart guy who "invented" (or discovered) a quick and smart method of calculatin all points along a line on the screen using only integers. He has also designed a very efficient circle and (as modificationof it) ellipse algorithm. It is independent of the used programming language.

DMN's solution is very simple: he uses a Windows function (LineDDA) which calls a given procedure for every position where a dot would be drawn on the line.
0
 

Author Comment

by:zerotwo
ID: 6241129
Ok I decided to accept the answers of zebada and DMN because they are quite easy to implement and answer the question "Now I need to know all the different points between (0,0) and (233,417)." much better then the rest of the comments. That's why I'm going to split points for zebada and DMN. zebada gave the basic information and DMN made an example, so each gets 50. Please wait, until points are splitted.
THX
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6426429
zerotwo, please have these points split as you promised to do. Thank you.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 8685308
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

To be split between zebada and DMN

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Thank you,
Russell

EE Cleanup Volunteer
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now