Asked by Jan Kappen
on 7 May 2015

Hey guys, this might be a simple problem but I'm going crazy here:

Assume you have two lines or direction vectors r,s from the point p (e.g. the origin):

p = [0 0 0]; r = [2 1 1]; s = [.2 0.1 .5];

Interpreting these vectors span a plane in R^3 (red vector is the normal vector):

This works fine with a normal meshgrid. What I really want is to create a grid "between" the r and s axis, and not a rectangular grid between the x and y values.

I can plot points along the vectors, so I have the coordinate for each star or diamond. Let's call them a and b (which are 3d vectors):

rn = r/norm(r); sn = s/norm(s);

a = linspace(p,p+rn,10);

b = linspace(p,p+sn,10);

plot3(a(1,:),a(2,:),a(3,:),'*')

plot3(b(1,:),b(2,:),b(3,:),'d')

How can I define a grid between a and b?

PS, this is only for visualization, I know that planes have an infinity spread ;)

Thank you very much! Jan

Answer by Matt J
on 7 May 2015

Edited by Matt J
on 18 May 2015

[i,j]=ndgrid(linspace(0,1,10));

xyz = bsxfun(@plus, p, i(:)*r + j(:)*s)

scatter3(xyz(:,1),xyz(:,2),xyz(:,3))

Jan Kappen
on 18 May 2015

Thank you! But this is "just" a replacement for the vectorized linspace version above, or did I get something wrong?

Anyways, in the end I realized, that a plane (for which the grid was used) between the two vectors does not look good, so I needed a rectangular plane which is parallel to the vectors, like:

Additionally a surf plot didn't look good, too. So I could simply create the meshgrid manually (planeSpread just makes the plane a bit larger):

normal = cross(r,s)/norm(cross(r,s));

d = dot(p,normal);

edge1 = p-planeSpread(1)*r-planeSpread(2)*s;

edge2 = p+planeSpread(1)*r-planeSpread(2)*s;

edge3 = p+planeSpread(1)*r+planeSpread(2)*s;

edge4 = p-planeSpread(1)*r+planeSpread(2)*s;

xx = [edge1(1) edge4(1); edge2(1) edge3(1)];

yy = [edge1(2) edge4(2); edge2(2) edge3(2)];

zz = (d-(xx*normal(1)+yy*normal(2))/normal(3); % x'*n0 = d -> x1*n1+x2*n2+x3*n3=d

mesh(xx,yy,zz)

Yes this is a bit ugly but at least I realized how the grid things work (I mean the structure of a meshgrid) and mesh does not need a rectangular grid (rectangular in the sense of being parallel to the coordinate axis)

Thank you.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Matt J (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/215538-nonrectangular-grid-between-two-lines#comment_283618

## Jan Kappen (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/215538-nonrectangular-grid-between-two-lines#comment_285502

Sign in to comment.