I need help with a morphing program.
    9 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
I am a first year student and i need some help with a program, i need to make a program that morphs 2D shapes and i do not know what to do further from this point. My code keeps giving me errors and for some reason to some other computers it doesn't open at all or just count frames(that is odd and idk why this happen) 
>> % Def parameters
numShapes = 9;
numFrames = 100;
colors = rand(numShapes, 3); % random colours for each shape
% Create a figure
figure;
for frame = 1:numFrames
    clf; % Clear figure
    for shapeIndex = 1:numShapes
        t = frame / numFrames; % interpolate shapes
        % Interpoleaza intre diferite forme
        currentShape = shape(shapeIndex, 1);
        nextShape = shape(mod(shapeIndex, numShapes) + 1, 1);
        x = (1 - t) * currentShape(:, 1) + t * nextShape(:, 1);
        y = (1 - t) * currentShape(:, 2) + t * nextShape(:, 2);
        % schimba culoare in functie de forma
        color = (1 - t) * colors(shapeIndex, :) + t * colors(mod(shapeIndex, numShapes) + 1, :);
        % Plot forma
        fill(x, y, color);
        hold on;
    end
    axis([-5, 5, -5, 5]); % for a better visuailsation
    axis equal;
    axis off;
    title(['Frame: ' num2str(frame)]);
    pause(0.1); % pause between frames
    switch index
        case 1 % triangle
            theta = linspace(0, 2 * pi, 3);
        case 2 % square
            theta = linspace(0, 2 * pi, 4);
        case 3 % pentagon
            theta = linspace(0, 2 * pi, 5);
        case 4 % hexagon
            theta = linspace(0, 2 * pi, 6);
        case 5 % heptagon
            theta = linspace(0, 2 * pi, 7);
        case 6 % octogon
            theta = linspace(0, 2 * pi, 8);
        case 7 % Nonagon
            theta = linspace(0, 2 * pi, 9);
        case 8 % Decagon
            theta = linspace(0, 2 * pi, 10);
        case 9 % Circle
            theta = linspace(0, 2 * pi, 100);
    end
    radius = shapeSize / 2;
    shapeData = [radius * cos(theta), radius * sin(theta)];
end
I do not want some one to solve it, rather i want to explain it for some one whom just started learning matlab. i really wana learn even if i do this for one semestre.
thx yall
0 comentarios
Respuestas (2)
  Steven Lord
    
      
 el 5 de En. de 2024
        My code keeps giving me errors
The full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
When I try running the code, I receive the error "Unrecognized function or variable 'shape'." Indeed, I see you use shape() to try to define currentShape and nextShape in your inner for loop but nowhere in the code you've posted do you define that function or variable.
Also, a couple potential enhancements for your code:
It wouldn't give exactly the same plot, but you might be interested in the nsidedpoly function for creating regular N-sided polygons. You could plot, rotate, scale, translate, etc. the object returned by nsidedpoly so their "first" points lie on the X axis.
Alternately you could use linspace to generate numbers between 0 and 2 (rather than 0 and 2*pi) and then use cospi and sinpi to compute the cosine or sine of pi times that vector of angles (without actually multiplying by pi.) Since pi does not return the full transcendental value of π but the double-precision value closest to it, as an example sin(pi) is not exactly 0 but sinpi(1) is exactly 0.
sin(pi)
sinpi(1)
0 comentarios
  Hassaan
      
 el 5 de En. de 2024
        function morphShapes
    numShapes = 9;
    numFrames = 100;
    colors = rand(numShapes, 3); % random colours for each shape
    shapeSize = 4; % Define a size for the shapes
    figure;
    for frame = 1:numFrames
        clf; % Clear figure
        for shapeIndex = 1:numShapes
            t = frame / numFrames; % interpolate shapes
            currentShape = getShape(shapeIndex, shapeSize);
            nextShape = getShape(mod(shapeIndex, numShapes) + 1, shapeSize);
            x = (1 - t) * currentShape(:, 1) + t * nextShape(:, 1);
            y = (1 - t) * currentShape(:, 2) + t * nextShape(:, 2);
            color = (1 - t) * colors(shapeIndex, :) + t * colors(mod(shapeIndex, numShapes) + 1, :);
            fill(x, y, color);
            hold on;
        end
        axis([-5, 5, -5, 5]); % for a better visualization
        axis equal;
        axis off;
        title(['Frame: ' num2str(frame)]);
        pause(0.1); % pause between frames
    end
end
% Placeholder for getShape function. Implement the shape logic here.
function shapeData = getShape(index, shapeSize)
    switch index
        case 1 % triangle
            theta = linspace(0, 2 * pi, 4);
        case 2 % square
            theta = linspace(0, 2 * pi, 5);
        % Add cases for other shapes...
        case 9 % Circle
            theta = linspace(0, 2 * pi, 100);
        otherwise
            theta = linspace(0, 2 * pi, 100); % Default circle
    end
    theta(end) = []; % Remove duplicate point at the end
    radius = shapeSize / 2;
    shapeData = [radius * cos(theta)', radius * sin(theta)'];
end
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
0 comentarios
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


