Make grid from patch object

5 次查看(过去 30 天)
proczell
proczell 2018-1-25
评论: proczell 2018-1-27
Hi
I am trying to make a function for splitting a patch object into N equally sized indexed rectangles (like a grid). The intended use is that the patch object contains the (x,y) values for a floor in a room, and I want to split the room into N number of rectangles which later will be used to make a "Region of Interest" in an optimization problem.
Any help would be highly appreciated.
  12 个评论
proczell
proczell 2018-1-26
Since you have shown interest in helping me, I though I would post the current code, which seems to have a good potential.
area = 45.020;
sq_area = area/50;
side = sqrt(sq_area);
ylim = 5;
xlim = 4;
i = 1;
origin = [0 ;0]
L = origin;
figure
bOK = true
while(bOK)
L = [origin(1) ;origin(2)+side];
B = [origin(1) + side ;origin(2)];
R = [origin(1) + side ;origin(2) + side];
if L(1) < xlim
if L(2) < ylim
cubes{i} = [origin B R L origin]
curr_cube = cubes{i}
hold on
plot(curr_cube(1,1:end),curr_cube(2,1:end),'b')
origin = [L(1) ;L(2)]
i = i + 1;
else
cubes{i} = [origin B R L origin]
curr_cube = cubes{i}
hold on
plot(curr_cube(1,1:end),curr_cube(2,1:end),'b')
origin = [B(1) ;0]
i = i + 1;
end
else
bOK = 0;
end
pause(0.5)
end
This will swipe along the y-axis, but could easily be extended to a possibility for swiping along the x-axis. The final problem to solve is to make xlim and ylim dynamic according to the given room, a problem I think is highly solvable.
proczell
proczell 2018-1-27
Another update. This works as intended, but if anyone finds improvements or errors, please let me know.
clear all ;close all; clc
x = [0 18.01 18 14.51 0];
y = [0 0 0 0 0];
z = [0 0 5.0 2.5 24];
% Patch object
nlin = 200;
for i = 1:length(x)
if i == length(x)
if x(i) == x(1)
x_l(i,1:nlin) = x(i);
y_l(i,1:nlin) = linspace(z(i),z(1),nlin);
else
xd = x(i) + x(1); zd = z(i) + z(1);
m = (z(i) - z(1))/(x(i) - x(1));
x_l(i,1:nlin) = linspace(x(i),x(1),nlin);
y_l(i,1:nlin) = m*x_l(i,1:nlin) - m*x(i) + z(i);
end
else
xd = x(i) + x(i+1); zd = z(i) + z(i+1);
m = (z(i) - z(i+1))/(x(i) - x(i+1));
x_l(i,1:nlin) = linspace(x(i),x(i+1),nlin);
y_l(i,1:nlin) = m*x_l(i,1:nlin) - m*x(i) + z(i);
end
end
y_l = [y_l(1,1:end) y_l(2,1:end) y_l(3,1:end) y_l(4,1:end),y_l(5,1:end)];
x_l = [x_l(1,1:end) x_l(2,1:end) x_l(3,1:end) x_l(4,1:end) x_l(5,1:end)];
% hold on
figure
p = patch(x,y,z,'r')
figure
scatter(x_l,y_l)
% Compute area
verts = get(p, 'Vertices');
faces = get(p, 'Faces');
a = verts(faces(:, 2), :) - verts(faces(:, 1), :);
b = verts(faces(:, 3), :) - verts(faces(:, 1), :);
c = cross(a, b, 2);
area = 1/2 * sum(sqrt(sum(c.^2, 2)));
% Fishnet generation
area = 45.020;
sq_area = area/50;
side = sqrt(sq_area);
ylim = 5;
xlim = 4;
i = 1;
origin = [0 ;0]
L = origin;
x_2 = round(x_l,1);
xlim = max(x_l)
bOK = true
while(bOK)
L = [origin(1) ;origin(2)+side];
B = [origin(1) + side ;origin(2)];
R = [origin(1) + side ;origin(2) + side];
L_2 = round(R(1),1);
x_current = find(x_2==L_2)
for i = 1:length(x_current)
y_vals = y_l(x_current);
ylim = max(y_vals)
end
if L(1) < xlim
if L(2) < ylim
cubes{i} = [origin B R L origin];
curr_cube = cubes{i};
hold on
plot(curr_cube(1,1:end),curr_cube(2,1:end),'k')
origin = [L(1) ;L(2)]
i = i + 1;
else
cubes{i} = [origin B R L origin];
curr_cube = cubes{i};
hold on
plot(curr_cube(1,1:end),curr_cube(2,1:end),'k')
origin = [B(1) ;0]
i = i + 1;
end
else
bOK = 0;
end
pause(0.03)
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Polygons 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by