### Highlights from Connect two pixels

3.69231
3.7 | 13 ratings Rate this file 5 Downloads (last 30 days) File Size: 2.6 KB File ID: #4211 Version: 1.0

# Connect two pixels

by

### Kanchi (view profile)

03 Dec 2003 (Updated )

Draw a line between two pixels with any grayscale in any image.

File Information
Description

Connect two pixels in an image with the desired graylevel

Command line
------------
result = func_DrawLine(Img, X1, Y1, X2, Y2)
input: Img : the original image.
(X1, Y1), (X2, Y2) : points to connect.
nG : the gray level of the line.
output: result

Note
----
Img can be anything
(X1, Y1), (X2, Y2) should be NOT be OUT of the Img

The computation cost of this program is around half as Cubas's [1]
[1] As for Cubas's code, please refer

Example
-------
result = func_DrawLine(zeros(5, 10), 2, 1, 5, 10, 1)
result =
0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1

MATLAB release MATLAB 6.5 (R13)
07 Jan 2016 Thijs Bruins Slot

### Thijs Bruins Slot (view profile)

Why so complex? This worked fine for me in 3D:

function img = Drawline3D(img, X0, Y0, Z0, X1, Y1, Z1)

for n = 0:(1/round(sqrt((X1-X0)^2 + (Y1-Y0)^2 + (Z1-Z0)^2))):1
xn = round(X0 +(X1 - X0)*n);
yn = round(Y0 +(Y1 - Y0)*n);
zn = round(Z0 +(Z1 - Z0)*n);
img(xn,yn,zn) = 1;
end

end

26 Jul 2013 Putra A

28 Jun 2010 Omar

### Omar (view profile)

Thanks for sharing.
I would suggest adding a line thickness option.

Comment only
23 Feb 2008 Jamie Schirf

Thanks for sharing. This was just what I needed for a task.

31 Jul 2007 Graham Cranston

input coordinates X and Y are implemented inverted. That is, the coordinates given as X0 and X1 are used as the starting and ending ROW in the matrix, and vice versa. A simple find replace fixed the problem.

22 Jun 2007 Rajesh Govind

I agree there exists jaggedness while it traces the line, but otherwise, I like the implementation :) Good work appreciated .. this saved my time as well ! Thanks a ton !

27 Apr 2007 cano lee

interesting, but could u disclose more technical details?

30 May 2006 Ed Zwaan

It doesn't draw a line straight between the pixels, it has some offset.

05 May 2006 Saeed Mozaffari

It works well

09 Aug 2005 kevino z

yeah, it seems that the line is always one pixel to the right and above the start/end points

Comment only
21 Nov 2004 Yuan luo

good

07 Jun 2004 Afshan naz
24 Apr 2004 chim wicket

very good, it save much time for me.

29 Mar 2004 user anonymous

Does n't work really well - the lines are broken and sometimes it doesn't connect the two pixels at all!!

03 Dec 2003 HY WANG