How to patch in a for loop?

2 views (last 30 days)
TOLUWALOPE AKINDOKUN
TOLUWALOPE AKINDOKUN on 28 Feb 2017
Answered: Anushka on 23 Jan 2025
i want to patch points to obtain a square and do it for all other points on the axis, but i want to do it in a for loop.. Thereafter, i would apply some transformation properties to the patched points. This is what i have done so far. Any help will be well appreciated.
whitebg('g')
axis on
% first patch point on the axis
pts2 = [0 0 1 1 0;0 1 1 0 0];
% last patch point on the axis
pts3 = [29 29 30 30 29;29 30 30 29 29];
[n m] = size(pts2);
[o p] = size(pts3);
axis([0 30 0 30])
shg
theta=0;
dx=0;
dy=0;
d=patch(pts1(1,1:end),pts1(2,1:end),'b*-');
for pts1 = pts2:pts3
if (d==patch(pts1(1,1:end),pts1(2,1:end),'b*-'))
delete(d)
end
%function to process
rot = [cosd(theta) sind(theta);-sind(theta) cosd(theta)];
trans = [1 0 dx;0 1 dy; 0 0 1];
homogeneous_rot = eye(3);
homogeneous_rot(1:2,1:2) = rot;
homogeneous_pts1 = [pts1; ones(1,5)];
trans_pts1 = trans*homogeneous_rot*homogeneous_pts1;
hold off
f=patch(trans_pts1(1,1:end),trans_pts1(2,1:end),'r*-');
draw now
end

Answers (1)

Anushka
Anushka on 23 Jan 2025
Hi Toluwalope Akindokun,
I understand that you want to create square patches along an axis, apply transformations, and display them using a for loop.Below is a revised version of your code. This script sets up the environment, iterates over grid points to create square patches, and applies rotation and translation transformations. It also includes an animation effect to visualize the transformations:
whitebg('g')
Warning: whitebg will be removed in a future release.
axis on
axis([0 30 0 30])
shg
% Define the square patch points relative to an origin
square_size = 1;
square_pts = [0 0 square_size square_size 0; 0 square_size square_size 0 0];
% Transformation parameters
theta = 0;
dx = 0.5;
dy = 0.5;
% Loop over grid points on the axis
for x = 0:5:25
for y = 0:5:25
translated_pts = square_pts + [x; y];
patch_handle = patch(translated_pts(1, :), translated_pts(2, :), 'b*-');
for step = 1:20
rot = [cosd(theta) sind(theta); -sind(theta) cosd(theta)];
trans = [1 0 dx; 0 1 dy; 0 0 1];
homogeneous_pts = [translated_pts; ones(1, size(translated_pts, 2))];
homogeneous_rot = eye(3);
homogeneous_rot(1:2, 1:2) = rot;
trans_pts = trans * homogeneous_rot * homogeneous_pts;
set(patch_handle, 'XData', trans_pts(1, :), 'YData', trans_pts(2, :));
% Update transformation parameters
theta = theta + 5;
dx = dx + 0.1;
dy = dy + 0.1;
% Pause to create animation effect
pause(0.1);
end
end
end
Here’s a link to the related documentation:
Hope this helps!

Community Treasure Hunt

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

Start Hunting!