How to make an angled line in a meshgrid

11 Ansichten (letzte 30 Tage)
Stef Kuiper
Stef Kuiper am 18 Mai 2020
Kommentiert: darova am 19 Mai 2020
I need to make an angled line (ultimately an angled square) in a meshgrid, but my matlab skills are low to non-excistent, sorry.
Suppose i have a 500x500 meshgrid with all values as 0. I need to make some rotated squares inside this meshgrid. Right now i can only make straight squares, so [y1:y2, x1:x2] = 1. However, i cannot figure out how to make rotated squares, i dont even know if this is possible in a meshgrid.
I know the length of the squares and the required angle, but how do i then make the line between two corner points of the square (and ultimately fill in the whole square). It doesn't matter if the lines are 'blocky', (then ill just increase the size of the meshgrid and the squares), or if the square isn't exactly the length needed (because there is no point in the meshgrid at that exact angle and squarewidth).
This is the code i am using so far (mostly copied from this thread https://nl.mathworks.com/matlabcentral/answers/329166-plotting-random-points-within-boundary) imageHeight and Width are the height and width of the meshgrid (in the case of the example 500), the squarewidth is the width of the square im making.
The xlim and ylim at the bottom are just there because thats the only part I need.
I hope someone understands my question and can help me further
% M-file to place multiple small squares inside a big circle where no squares overlap, though they might touch.
% Clean up
close all;
clc;
fontSize = 15;
% Initialize some parameters.
numberOfsquares = 150000; % Number of small squares
squareInsideValue = 0; % black squares
circleOutsideValue = 1; % white circle
circleInsideValue = 1; % white circle
squareWidth = 12;
imageWidth = 500;
imageHeight = 500; % square area 0f 500*500
circleRadius = 250; % big circle radius
% Initialize an image to hold one single big circle.
ourImage = circleOutsideValue * ones(imageHeight, imageWidth);
[x, y] = meshgrid(1:imageWidth, 1:imageHeight);
ourImage((x - imageWidth/2).^2 + (y - imageHeight/2).^2 <= circleRadius.^2) = circleInsideValue;
% Display it in the upper left plot.
%subplot(2, 1, 1);
%imshow(ourImage, []);
%title('Circle Mask', 'FontSize', fontSize);
%set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Place the small squares inside one by one.
for k = 1 : numberOfsquares
% Get a random row, column location
y1 = randi(imageHeight, 1);
x1 = randi(imageWidth, 1);
y2 = y1 + squareWidth - 1;
x2 = x1 + squareWidth - 1;
% See if any of those corners are outside the circle. Skip this square if so.
if sqrt((x1 - imageWidth/2)^2 + (y1 - imageHeight/2)^2) > circleRadius
continue;
end
if sqrt((x1 - imageWidth/2)^2 + (y2 - imageHeight/2)^2) > circleRadius
continue;
end
if sqrt((x2 - imageWidth/2)^2 + (y1 - imageHeight/2)^2) > circleRadius
continue;
end
if sqrt((x2 - imageWidth/2)^2 + (y2 - imageHeight/2)^2) > circleRadius
continue;
end
% If you get to here it's inside the circle.
% Now make sure no corner is inside any square that has already been placed.
if ourImage(y1, x1) == squareInsideValue
continue;
end
if ourImage(y1, x2) == squareInsideValue
continue;
end
if ourImage(y2, x1) == squareInsideValue
continue;
end
if ourImage(y2, x2) == squareInsideValue
continue;
end
% If we get here, we're free to place the square.
ourImage(y1:y2, x1:x2) = squareInsideValue;
end
% Display it in the lower left plot.
%subplot(2, 1, 2);
figure(2), clf(2);
imshow(ourImage);
%caption = sprintf('Now with %d Squares Added', numberOfsquares);
%title(caption, 'FontSize', fontSize);
xlim([170 195])
ylim([330 342.5])
% Note: squares might not be overlapping but they might be touching.

Akzeptierte Antwort

darova
darova am 18 Mai 2020
Here is an idea:
  • create 4 points (corners of your rectangle) and some background
x = [...]
y = [...]
A = ones(50); % background (empty image)
  • create mask of rectangle region using impoly
p = impoly(gca,[x y]);
imshow(p.createMask)
  6 Kommentare
Stef Kuiper
Stef Kuiper am 19 Mai 2020
thank you so very much!
darova
darova am 19 Mai 2020

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interactions, Camera Views, and Lighting finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by