Having trouble translating a polygon.

2 views (last 30 days)
Phillip Raush
Phillip Raush on 10 Mar 2021
Answered: Zinea on 23 Feb 2024
So I have plane. I need to traslate this plane to a few different points, taking 10 steps to get to the next point. My problem is that my trasnformation matrix is not the same size as the plane matrix. I am wondering how I can make this work.
Any help is appreciated.
here is my code.
clear all; close all;
x0 = 2; % defining the starting point in the coordinate system
y0 = -3;
x_plane = [0,1,1.5, 2,2.5,2.5,1.5,3,4,4,0]+x0; % building plane at the starting point
y_plane = [0,1,1.0,-1,-1.,1.0,1.0,1,2,0,0]+y0;
plane = [x_plane;y_plane;ones(size(x_plane))]; % creating array of x,y positions
%h = plot_polygon(plane); % pushing vector of positions into function
axis equal; grid on; % setting axis parameters
%set(h(1),'Marker','x','MarkerEdgeColor','k') % Setting markers to show specified points
% x and y traslations
x = [linspace(0,10,10),linspace(10,10,10),linspace(10,0,10)];
y = [linspace(0,0,10),linspace(0,10,10),linspace(10,0,10)];
for i = 1:1:length(x)
T = [1,0,x(i); 0,1,y(i); 0,0,1] %trasnformation matrix
planeNEW = plane * T
plot_polygon(planeNEW); % pushing vector of positions into function
axis equal; grid on; % setting axis parameters
clf
pause(0.5)
end

Answers (1)

Zinea
Zinea on 23 Feb 2024
  • You trying to translate a 2D plane shape to different points using a transformation matrix which is of size 3x3.
  • The transformation matrix you have is a 3x3matrix, which is the correct size for affine transformation when using homogeneous coordinates.
  • The plane matrix is also correctly structured to be multiplied by the transformation matrix T.
However, there are a few issues with your code:
  1. You are multiplying the ‘plane’ matrix by the transformation matrix ‘T’ in the wrong order. The correct order is to multiply ‘T’ by ‘plane’ from the left, no the right.
  2. You are using ‘plot_polygon(planeNEW)’ without showing the implementation of the function. I am assuming this function plots the polygon defined by the input matrix.
  3. For the translation to look like an animation, you should clear the figure before the next iteration.
You can refer to the code for the loop given below:
% Animate the plane translation
for i = 1:length(x)
T = [1, 0, x(i); 0, 1, y(i); 0, 0, 1]; % Transformation matrix
planeNEW = T * plane; % Apply the transformation
plot_polygon(planeNEW); % Plot the transformed plane
pause(0.5);
if i < length(x)
clf; % Clear the figure for the next iteration
end
end

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!