# Creating a grid on an image at an angle with a specific spacing

4 views (last 30 days)
Bera on 4 Jun 2024
Commented: Bera on 5 Jun 2024
Hey All
The summary is it all. Basically I have an image and a code that draws two "axes" lines a red and blue line. I basically want to form a grid using lines parellel to those in order to find their intersects and plot other circles where they intersect.
I have provided an image for clarification!

Amish on 5 Jun 2024
Hi Bera,
Assuming that your axes could be at an angle from the default x-y axes, you can follow the following approach to plot the required grid pattern:
1. Plot the original axes (Red and Blue line)
% Original line equations: y = m1*x + b1 and y = m2*x + b2
m1 = 1;
b1 = 0;
m2 = -1;
b2 = 0;
% Define plot range for x
x_range = linspace(-10, 10, 400);
% Calculate y values based on the line equations
y1 = m1 * x_range + b1;
y2 = m2 * x_range + b2;
% Plot the lines
figure;
hold on;
plot(x_range, y1, 'r');
plot(x_range, y2, 'b');
axis equal;
2. Generate parallel lines at a fixed distance away from the original lines. For this, use a method that shifts the lines by adjusting the intercept (b).
% Define the number of parallel lines and their separation
num_lines = 5; % Num lines on each side of the original line
sep = 1; % Separation distance
% Generate and plot parallel lines for the first line
for i = -num_lines:num_lines
b_shift = b1 + i * sep;
y_shift = m1 * x_range + b_shift;
plot(x_range, y_shift, 'LineStyle', '-', 'Color', [0.8, 0.5, 0.5]);
end
% Generate and plot parallel lines for the second line
for i = -num_lines:num_lines
b_shift = b2 + i * sep;
y_shift = m2 * x_range + b_shift;
plot(x_range, y_shift, 'LineStyle', '-', 'Color', [0.5, 0.5, 0.8]);
end
3. Finally, to find intersections between two sets of lines, you can use the fact that at the intersection, the values are equal. So, (), which can be solved for . These can then be plotted.
% Find intersections between sets of parallel lines
for i = -num_lines:num_lines
for j = -num_lines:num_lines
% Calculate new intercepts
b1_shift = b1 + i * sep;
b2_shift = b2 + j * sep;
% Solve for x and y to find intersection
x_intersect = (b2_shift - b1_shift) / (m1 - m2);
y_intersect = m1 * x_intersect + b1_shift;
% Plot circle at intersection
viscircles([x_intersect, y_intersect], 0.2, 'Color', 'k', 'LineWidth', 0.5);
end
end
hold off;
The final graphs might look like:
Hope this helps!
Bera on 5 Jun 2024
Thank you

### Categories

Find more on Blue in Help Center and File Exchange

R2024a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!