View Single Post
  #8   Spotlight this post!  
Unread 17-10-2005, 20:29
Tristan Lall's Avatar
Tristan Lall Tristan Lall is offline
Registered User
FRC #0188 (Woburn Robotics)
 
Join Date: Aug 2001
Rookie Year: 1999
Location: Toronto, ON
Posts: 2,484
Tristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond repute
Re: Borland Delphi 6

You're actually doing too much work, assuming that you don't need to show (your own) code for actually generating the line; Delphi allows you to just do this:
Code:
Form1.Canvas.MoveTo(x1, y1);
Form1.Canvas.LineTo(x2, y2);
Which is actually the only important part of this:
Code:
unit Unit1;
{$O-} //Directive to turn off optimization of variables, for debugging only
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
 
type
TForm1 = class(TForm)
	Edit1: TEdit;
	Edit2: TEdit;
	Edit3: TEdit;
	Edit4: TEdit;
	Button1: TButton;
	Label1: TLabel;
	Label2: TLabel;
	Label3: TLabel;
	Label4: TLabel;
	Memo1: TMemo;
	ColorDialog1: TColorDialog;
	Button2: TButton;
	procedure Button1Click(Sender: TObject);
	procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
function DrawLine(x1, y1, x2, y2: Integer): Boolean;
implementation
 
{$R *.dfm}
function DrawLine(x1, y1, x2, y2: Integer): Boolean;
begin
if (x1 <= Form1.ClientWidth) AND (x1 >= 0) AND //Checking boundaries
	 (x1 <= Form1.ClientWidth) AND (x2 >= 0) AND //(not strictly
	 (y2 <= Form1.ClientHeight) AND (y1 >= 0) AND //necessary, but often
	 (y2 <= Form1.ClientHeight) AND (y2 >= 0)	 //a good idea)
	then
	 begin
		Form1.Canvas.MoveTo(x1, y1); //These two lines are all
		Form1.Canvas.LineTo(x2, y2); //you really need
		Result := True;
	 end
	else
	 Result := False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if DrawLine(StrToInt(Edit1.Text), StrToInt(Edit2.Text), StrToInt(Edit3.Text),
			 StrToInt(Edit4.Text))
	then
	 Memo1.Text := Concat('(', Edit1.Text, ', ', Edit2.Text, '), (',
							 Edit3.Text, ', ', Edit4.Text, ')')
	else
	 Memo1.Text := 'Line out of bounds';
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if ColorDialog1.Execute then Canvas.Pen.Color := ColorDialog1.Color;
end;
 
end.
This code also includes definitions for the functions, range checking, and (as extra stuff) a colour picker dialog (as an example of how to use the built-in functions) and a little feedback for the status of the line. See the attachments for what it looks like. The colour picker is on the controls toolbar under "Dialogs"—drop it on the form like a button to use it.

With Delphi, as with most other "visual" languages, a lot of code is devoted to defining the controls on the form. Also, some code is devoted to error-checking, not because it's strictly needed, but because good coding style dictates that you reject out-of-range values (unless you know exactly what they will do).

I'll leave it to you to implement the program by choosing what you want to do with the code above (because it's not exactly what you asked for), but it's really just a matter of placing the appropriate controls on the form, and taking a good look at the help files regarding the properties of each control that you use.

Also, remember that this is just the shortcut method that I've demonstrated; the algorithm you were working on earlier is probably pretty similar to what the LineTo() procedure implements, except that the source code for LineTo() is hidden in the gdi32.dll file, somewhere where you can't see it. If you're assigned the task "draw a line in Delphi", LineTo() will work. If you're assigned the task "implement an algorithm to draw a line in Delphi", you should continue with the algorithm you were developing earlier, because in that case, the real assignment is the creation of the logic, not the line itself.
Attached Thumbnails
Click image for larger version

Name:	lines2.jpg
Views:	20
Size:	14.4 KB
ID:	3622  Click image for larger version

Name:	lines2_design.jpg
Views:	19
Size:	69.4 KB
ID:	3623  

Last edited by Tristan Lall : 17-10-2005 at 20:40.