scaling issue for my surface

Here is my code:
------------------
clc;clear all; close all;
myaxes=axes('xlim', [-200,200], 'ylim', [-200,200], 'zlim', [-200, 200]);
grid on; axis equal;view(130,21);
I=imread('road.jpg');
[X, Y] = meshgrid(-200:200, -200:200);Z=zeros(size(X));
ground=surf(X,Y,Z-10, I,'edgecolor', 'none','FaceColor','texturemap','parent',myaxes);
hold on; xlabel('x'); ylabel('y');zlabel('z');
[xcy,ycy,zcy]=cylinder([1,1],20);
h(1)=surface(zcy*5,ycy*5,xcy*5, 'facecolor','yellow','parent',myaxes);
-----------------------------------------------
Every time i run the above script; the z-axis keep re-scaling based on the max-z value of my surface "h(1)", even though i made "myaxes" its parent!!! any suggestions is much appricated.

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 12 月 31 日

0 投票

myaxes = axes('xlim', [-200,200], 'ylim', [-200,200], 'zlim', [-200, 200]);
grid(myaxes, 'on'); axis(myaxes, 'equal'); view(myaxes, 130,21); %changed
I = imread('road.jpg');
[X, Y] = meshgrid(-200:200, -200:200); Z = zeros(size(X));
ground = surf(X,Y,Z-10, I,'edgecolor', 'none','FaceColor','texturemap','parent',myaxes);
hold(myaxes, 'on'); xlabel(myaxes, 'x'); ylabel(myaxes, 'y'); zlabel(myaxes, 'z'); %changed
[xcy,ycy,zcy] = cylinder([1,1],20);
h(1) = surface(zcy*5,ycy*5,xcy*5, 'facecolor','yellow','parent',myaxes);
And if that still does not work then in your creation of myaxes add the parameter 'zlimmode', 'manual'

6 件のコメント

AWNI ALSHAKHSHIR
AWNI ALSHAKHSHIR 2015 年 12 月 31 日
Waltor , you are awesome!!! it worked brother. you will be my matlab mentor and to go dude every time i have a matlab question. many thanks for your prompt answer. i really appreciate the time you took to answer my question.
i am working on a project in which i try to command a car to move in a random racetrack based on my input. the input format will be guidelines drawn by me "the user" . so the program should start by loading your own racetrack, draw your own guidelines on the track and intiate the car movement based on your guideline inputs. the difficult part for me right now is how to draw lines on an image for a random racetrack and extract the x,y coordinates for these lines. any insight with this matter will be much appreciated brother. thanks again for your continuous help and support.
Walter Roberson
Walter Roberson 2015 年 12 月 31 日
See imfreehand(), and impoly() with 'closed', false
Here is a trick: if you get a set of vertices:
V = getposition(impoly('Closed', false));
then supposing you want to work in an M x N array,
V2 = [V; V(end-1:-1:1)];
mask = poly2mask(V2(:,1), V2(:,2), M, N);
Then mask will be true for each pixel that the lines go through.
Reversing V together with the reverse of V defines a 1 pixel thick line. If you had used just V then the first pixel would be connected to the last pixel automatically which is not going to be desirable.
AWNI ALSHAKHSHIR
AWNI ALSHAKHSHIR 2016 年 1 月 4 日
編集済み: Walter Roberson 2016 年 1 月 4 日
Happy new year walter,
i resolved the input format for my program but when i tried to intiate the motion of the wheels, they dont appear in the view. here is the new code;
Revised Code: -------------
%input format:
%###################
clc;clear all;close all;figure;
myaxes=axes('xlim', [-200,200], 'ylim', [-200,200],'zlim',[-4,100]);
grid(myaxes, 'on'); axis(myaxes, 'equal'); view(myaxes, 0,90);
hold(myaxes, 'on'); xlabel(myaxes, 'x'); ylabel(myaxes, 'y'); zlabel(myaxes, 'z');
I=imread('road.jpg');
[X, Y] = meshgrid(-200:200, -200:200);Z=zeros(size(X));
ground=hggroup;ground=surf(X,Y,Z-1, I,'edgecolor', 'none','FaceColor','texturemap');
ground=hggroup;set(ground,'parent',myaxes);
N=input('how many points u want on the car track =');
for i=1:N
H = impoint(ground);
pos = getPosition(H);
xposition(i)=[pos(1)];
yposition(i)=[pos(2)];
end
plot(xposition,yposition);
%car motion initiation (only translation allowed)
%###################################################
[xcy,ycy,zcy]=cylinder([1,1],20);
h(1)=surface(xcy,zcy+170,ycy,'facecolor','yellow','parent',myaxes);
h(2)=surface(xcy,zcy+4+170,ycy,'facecolor','yellow','parent',myaxes);
combinedobject1=hgtransform('parent',myaxes);combinedobject2=hgtransform('parent',myaxes);
set(h,'parent',combinedobject1); H=copyobj(h,combinedobject2);
Txy = makehgtform('translate',[4 0 0]);
set(combinedobject2,'Matrix',Txy);
drawnow
xtranslation=xposition;ytranslation=yposition;
%bearing=[0:20:5*360];
for i=1:length(xtranslation)
translation=makehgtform('translate',[xtranslation(i),ytranslation(i) 0]);
% rotation1=makehgtform('yrotate',(pi/180)*(bearing(i)));
%rotation2=makehgtform('zrotate',(pi/180)*(bearing(i)));
set(combinedobject1,'matrix',translation);
%rotation2*rotation1);
%rotation3=makehgtform('xrotate',(pi/180)*(bearing(i)));
set(combinedobject2,'matrix',Txy*translation);
%rotation2*rotation1);
pause(1)
end
%####################
you input on this matter is highly appreciated. thanks in advance
Walter Roberson
Walter Roberson 2016 年 1 月 4 日
I do not have the toolbox that defines impoint so I cannot test your code. I do notice
ground=hggroup;ground=surf(X,Y,Z-1, I,'edgecolor', 'none','FaceColor','texturemap');
ground=hggroup;set(ground,'parent',myaxes);
should be
ground_group = hggroup('Parent', myaxes);
ground = surf(X,Y,Z-1, I, 'edgecolor', 'none', 'FaceColor', 'texturemap', 'Parent', ground_group);
AWNI ALSHAKHSHIR
AWNI ALSHAKHSHIR 2016 年 1 月 7 日
sorry for taking so long to reply back, unfortunately my every time i run my code, matlab crashes(access violation) i think that my combination of impoint plus using image as a background of my surface is the problem. so i am trying to run the same code without using image as texture and finalize the motion of the wheels first then go back and seek alternative for my road input. here is the new code:
clc;clear all;close all;figure; myaxes=axes('xlim', [-200,200], 'ylim', [-200,200],'zlim',[-200,200]); grid(myaxes, 'on'); axis(myaxes, 'equal'); view(myaxes, 0,90); hold(myaxes, 'on'); xlabel(myaxes, 'x'); ylabel(myaxes, 'y'); zlabel(myaxes, 'z'); [X, Y] = meshgrid(-200:200, -200:200);Z=zeros(size(X)); ground_group = hggroup('Parent', myaxes); ground = surf(X,Y,Z-2,'edgecolor', 'none','facecolor','g','Parent', ground_group); N=input('how many points u want on the car track ='); for i=1:N H = impoint(myaxes ,[]); pos = getPosition(H); xposition(i)=[pos(1)]; yposition(i)=[pos(2)]; end for i=2:N deltax=[xposition(i)-xposition(i-1)]; deltay=[yposition(i)-yposition(i-1)]; end deltax=[0,deltax];deltay=[0,deltay]; plot(xposition,yposition); %car wheel formation %########################## [xcy,ycy,zcy]=cylinder([1,1],20); h(1)=surface(2*(zcy+xposition(1)),2*(ycy+yposition(1)),2*xcy,'facecolor','yellow','parent',ground_group); h(2)=surface(2*(zcy+xposition(1)+4),2*(ycy+yposition(1)),2*xcy,'facecolor','yellow','parent',ground_group); combinedobject1=hgtransform('parent',ground_group);combinedobject2=hgtransform('parent',ground_group); set(h,'parent',combinedobject1); H=copyobj(h,combinedobject2); Txy = makehgtform('translate',[0 8 0]); set(combinedobject2,'Matrix',Txy); drawnow ------------------------ I am trying to make the position of my four wheels spatially coincides with my first input point. but the wheels appear outsides of axes limit and if itry to rotate my axes to locate them they disappear.
please your input is very valuable. thanks in advance brother
AWNI ALSHAKHSHIR
AWNI ALSHAKHSHIR 2016 年 1 月 7 日
Here a nicer looking version of my code
************************************
clc;clear all;close all;figure;
myaxes=axes('xlim', [-200,200], 'ylim', [-200,200],'zlim',[-200,200]);
grid(myaxes, 'on'); axis(myaxes, 'equal'); view(myaxes, 0,90);
hold(myaxes, 'on'); xlabel(myaxes, 'x'); ylabel(myaxes, 'y'); zlabel(myaxes, 'z');
[X, Y] = meshgrid(-200:200, -200:200);Z=zeros(size(X));
ground_group = hggroup('Parent', myaxes);
ground = surf(X,Y,Z-2,'edgecolor', 'none','facecolor','g','Parent', ground_group);
N=input('how many points u want on the car track =');
for i=1:N
H = impoint(myaxes ,[]);
pos = getPosition(H);
xposition(i)=[pos(1)]; yposition(i)=[pos(2)];
end
for i=2:N deltax=[xposition(i)-xposition(i-1)]; deltay=[yposition(i)-yposition(i-1)];
end
deltax=[0,deltax];deltay=[0,deltay];
plot(xposition,yposition);
%car wheel formation %##########################
[xcy,ycy,zcy]=cylinder([1,1],20);
h(1)=surface(2*(zcy+xposition(1)),2*(ycy+yposition(1)),2*xcy,'facecolor','yellow','parent',ground_group); h(2)=surface(2*(zcy+xposition(1)+4),2*(ycy+yposition(1)),2*xcy,'facecolor','yellow','parent',ground_group);
combinedobject1=hgtransform('parent',ground_group);combinedobject2=hgtransform('parent',ground_group);
set(h,'parent',combinedobject1); H=copyobj(h,combinedobject2);
Txy = makehgtform('translate',[0 8 0]);
set(combinedobject2,'Matrix',Txy);
drawnow

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

質問済み:

2015 年 12 月 31 日

コメント済み:

2016 年 1 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by