Plotting a moving circle.

I need a circle to follow the end of a line to represent a wheel. However when I plot the center of the circle to be farther down but still revolving around xr3 and yr3 it shifts the center of the revolving radius to another point higher up than what is specified.
xr3=-10
yr3=0
r4m=linspace(degtorad(theta4),3*pi/2,100)
thetawl1=(r4+2)*cos(r4m)+xr3
thetawl2=(r4+2)*sin(r4m)+yr3
xcircle=(r4+5)*cos(r4m)+xr3
ycircle=(r4+5)*sin(r4m)+yr3
for i=1:1:length(r4m)
plot([xr3,thetawl1(i)],[yr3,thetawl2(i)],'r-')
hold on
wheel=rectangle('Position',[xcircle(i) ycircle(i) 6 6],'Curvature',[1,1],'LineWidth',10)
hold off
axis equal
axis ([-15 10 -20 5])
getframe
end

Risposte (1)

Walter Roberson
Walter Roberson il 14 Feb 2018
We recommend against plotting and replotting within a loop. We recommend that you instead build the graphics objects before the loop, and then in the loop, update the properties of the graphics objects. Or sometimes it is easier to write, for example:
xr3=-10
yr3=0
r4m=linspace(degtorad(theta4),3*pi/2,100)
thetawl1=(r4+2)*cos(r4m)+xr3
thetawl2=(r4+2)*sin(r4m)+yr3
xcircle=(r4+5)*cos(r4m)+xr3
ycircle=(r4+5)*sin(r4m)+yr3
for i=1:1:length(r4m)
if i == 1
L1 = plot([xr3,thetawl1(i)],[yr3,thetawl2(i)],'r-')
hold on
wheel = rectangle('Position',[xcircle(i) ycircle(i) 6 6],'Curvature',[1,1],'LineWidth',10)
hold off
axis equal
axis ([-15 10 -20 5])
else
set(L1, 'XData', [xr3,thetawl1(i)], 'YData', [yr3,thetawl2(i)]);
set(wheel, 'Position', [xcircle(i) ycircle(i) 6 6]);
end
getframe
end

6 Commenti

Stephen Taylor
Stephen Taylor il 14 Feb 2018
How would I go about trying this in order to fix the displacement of the circle? From the given code, it creates a loop in which it reaches its first full cycle and then resets to its initial position. My original issue is that by specifically setting the width and height of the circle to both be 6, it also places the circle where two sides rest at x=6 and y=6.
Walter Roberson
Walter Roberson il 14 Feb 2018
I do not understand. A diagram would help, along with sufficient data to be able to test with.
The code is rather long as there were multiple variables to calculate and it's likely not the cleanest or user friendly code. I've specified where the actual coding starts for the line and circle. You'll see that they move in about the same arc, but it appears it's placing the center of the circular path it's travelling higher than what is specified by xr3 and yr3.
p1x=6
p1y=0
p2x=-0.946699141100893
p2y=-4.247086478782448
p3x=2.428700058734363
p3y=-5.483475738094875
p4x=-5.055787223379352
p4y=-8.692224112360492
p5x=-4.242640687119286
p5y=-4.242640687119286
p6x=-10
p6y=-10
g1x=-20
g1y=-18.3
g2x=10
g2y=-18.3
m1=(p3y-p1y)/(p3x-p1x)
midx1=(p3x+p1x)/2
midy1=(p3y+p1y)/2
b1=(midx1/m1)+midy1
y1=(1/(-m1))*(-15-midx1)+midy1
m2=(p5y-p3y)/(p5x-p3x)
midx2=(p5x+p3x)/2
midy2=(p5y+p3y)/2
b2=(midx2/m2)+midy2
y2=(1/(-m2))*(10-midx2)+midy2
ix1=(b2-b1)/(m1-m2)
iy1=(m1*ix1)+b1
plot([ix1],[iy1],'g*')
r1=(((ix1-(-10))^2)+(iy1-0)^2)^.5
r2=(((p1x-ix1)^2)+(p1y-iy1)^2)^.5
r3=(((p2x-p1x)^2)+(p2y-p1y)^2)^.5
r4=(((10+p2x)^2)+(p2y-0)^2)^.5
theta2=-180+(atand(-iy1/p1x))
theta2actual=(atand(-iy1/p1x))
A=2*r4*(r1-r2*cosd(theta2))
B=-2*r2*r4*sind(theta2)
C=r3^2-r2^2-r4^2-r1^2+2*r2*r1*cosd(theta2)
u=((B+((A^2)+(B^2)-(C^2))^.5)/(C+A))
theta4=(2*atand(u))+180
x3=((-r2*cosd(theta2))+(r4*cosd(theta4))+r1)/r3
y3=((-r2*sind(theta2))+(r4*sind(theta4)))/r3
theta3=(atan2d(y3,x3))
%Line and circle plotting begins here.
xr3=-10
yr3=0
r4m=linspace(degtorad(theta4),3*pi/2,100)
thetawl1=(r4+2)*cos(r4m)+xr3
thetawl2=(r4+2)*sin(r4m)+yr3
xcircle=(r4+5)*cos(r4m)+xr3
ycircle=(r4+5)*sin(r4m)+yr3
for i=1:1:length(r4m)
plot([xr3,thetawl1(i)],[yr3,thetawl2(i)],'r-')
hold on
wheel=rectangle('Position',[xcircle(i) ycircle(i) 6 6],'Curvature',[1,1],'LineWidth',10)
hold off
axis equal
axis ([-15 10 -20 5])
getframe
end
Walter Roberson
Walter Roberson il 14 Feb 2018
rectangle()'s Position is not the center and radius. The Position for rectangle() is the lower left corner of the enclosing bounding box, with the center of [x y w h] being at (x+w/2, y+h/2)
Stephen Taylor
Stephen Taylor il 15 Feb 2018
So then would viscircle be a better function to use to plot what I need then?
Walter Roberson
Walter Roberson il 15 Feb 2018
Yes, viscircle sounds good.

Accedi per commentare.

Categorie

Richiesto:

il 14 Feb 2018

Commentato:

il 15 Feb 2018

Community Treasure Hunt

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

Start Hunting!

Translated by