# Draw a straight line in image, given an angle

243 views (last 30 days)
Philip on 29 Sep 2011
Commented: Walter Roberson on 21 Jul 2021
Is there a way to draw a straight line in an image from a starting (x,y) coordinate, such that the line is equivalent to a specified angle?
For example, if I start at pixel (50,50), how can I draw a line to be 30 degrees of this pixel? I would like to draw 10 pixels in this direction (obviously, the resulting line will be zig-zagged)...
Philip on 30 Sep 2011
Thanks for your kind suggestions. Mathematics is not my strong point, but your comments have helped me to get the otherwise unknown coordinates. I will now see if I can implement Bresenham's line algorithm to connect the two points!
Much appreciated!

Image Analyst on 29 Sep 2011
lineLength = 10;
angle = 30;
x(1) = 50;
y(1) = 50;
x(2) = x(1) + lineLength * cosd(angle);
y(2) = y(1) + lineLength * sind(angle);
hold on; % Don't blow away the image.
plot(x, y);
xlim([0 70]);
ylim([0 70]);
grid on;
If you want the line to be "burned into" your image, then you'll have to use imline() - search answers for my demo on that - or use the Bresenham line algorithm like Walter suggested if you want to do it yourself.
Walter Roberson on 21 Jul 2021
Michelle De Luna:
No. It is possible to generate two lines of identical length only if they happen to have a particular Pythagorean triple relationship such as listed at http://oeis.org/A084646 or related sequences. For example if the distance is 25 then there are two different angles that can generate that identical distance.
Any case in which the angle and distance does not happen to correspond to one of the triples, you can never get out identical distances.

Image Analyst on 30 Sep 2011
% Demo to write an ellipse and a line into the overlay of an image,
% and then to burn those overlays into the image.
%----- Initializing steps -----
% Clean up
clc;
clear all;
close all;
workspace; % Display the workspace panel.
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
% User said No, so exit.
return;
end
end
% Display images to prepare for the demo.
subplot(2, 4, 1);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 2);
imshow(monochromeImage);
title('Original Image with ellipse in overlay');
subplot(2, 4, 5);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 6);
imshow(monochromeImage);
title('Original Image with line in overlay');
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
%----- Burn ellipse into image -----
% Create elliptical mask, h, as an ROI object over the second image.
subplot(2, 4, 2);
hEllipse = imellipse(gca,[10 10 50 150]); % Second argument defines ellipse shape and position.
% Create a binary image ("mask") from the ROI object.
subplot(2, 4, 3);
imshow(binaryImage);
% Let's try to add some text. (Doesn't work)
% hText = text(50, 100, 'Line of Text');
% binaryImage = binaryImage & textMask;
% imshow(binaryImage);
% Burn ellipse into image by setting it to 255 wherever the mask is true.
monochromeImage(binaryImage) = 255;
% Display the image with the "burned in" ellipse.
subplot(2, 4, 4);
imshow(monochromeImage);
title('New image with ellipse burned into image');
%----- Burn line into image -----
% Create line mask, h, as an ROI object over the second image in the bottom row.
subplot(2, 4, 6);
hLine = imline(gca,[10 100],[10 100]); % Second argument defines line endpoints.
% Create a binary image ("mask") from the ROI object.
subplot(2, 4, 7);
imshow(binaryImage2);
% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage(binaryImage2) = 255;
% Display the image with the "burned in" line.
subplot(2, 4, 8);
imshow(burnedImage);
title('New image with line burned into image');