3D point source distance from a plane, and representing solution as a heat map

29 views (last 30 days)
Im trying to determine the distance from a point coordinate in 3D space, to a surface. My goal is to represent the distance as a heat map or some sort of plot, which will show the "intensity". (Eventually, I will add a few equations to simulate light decay and then repeat with multiple point sources).
So I make a plane, and I make a point source. I can find the distance between the point source and a point on the surface, using dist=norm(star-p1).
x1=[-5 5 5 -5];
y1=[0 0 0 0];
z1=[0 0 10 10];
hold on
%2D Plane
patch(x1,y1,z1, [0.71 0.72 .73]);
xlabel('x');
ylabel('y');
zlabel('z');
axis equal
%Point Source
star1=[0,5,5];
plot3(star1(1),star1(2), star1(3),'*','color','r','MarkerSize',12)
%Distance to wall
p1=[3,0,5] %Make a point on the wall
dist=norm(star1-p1) %find the distance from p1 to star
%view setup
rotate3d on
view(-200,15);
My thinking is I need to make some sort of loop, where it makes a point coordinate along the 2D plane, and then calculates the distance, and saves this in a 2D array. (this is a big programming feat for me...)
However, is there an easier way? perhaps some function I'm oblivious to?
thanks!
  1 Comment
philio63
philio63 on 21 May 2022
Ok, I figured out how to "calculate the distance" and then record this on a matrix. This may be a bit sloppy:
for ii = -5:5
hold on
for jj = 0:10
px=[ii,0,jj];
plot3(px(1),px(2), px(3),'o','color','b');
distance(ii+6,jj+1)=norm(star1-px)
end
end
So I make an array of the distances, row and colums 10 x 10.
  1. Now i think, how to overlay this on the 2D plane in matlab
  2. surely I'm not doing this correctly, or effeciently
My output for "distance":
8.7 8.1 7.7 7.3 7.1 7.1 7.1 7.3 7.7 8.1 8.7
8.1 7.5 7.1 6.7 6.5 6.4 6.5 6.7 7.1 7.5 8.1
7.7 7.1 6.6 6.2 5.9 5.8 5.9 6.2 6.6 7.1 7.7
7.3 6.7 6.2 5.7 5.5 5.4 5.5 5.7 6.2 6.7 7.3
7.1 6.5 5.9 5.5 5.2 5.1 5.2 5.5 5.9 6.5 7.1
7.1 6.4 5.8 5.4 5.1 5.0 5.1 5.4 5.8 6.4 7.1
7.1 6.5 5.9 5.5 5.2 5.1 5.2 5.5 5.9 6.5 7.1
7.3 6.7 6.2 5.7 5.5 5.4 5.5 5.7 6.2 6.7 7.3
7.7 7.1 6.6 6.2 5.9 5.8 5.9 6.2 6.6 7.1 7.7
8.1 7.5 7.1 6.7 6.5 6.4 6.5 6.7 7.1 7.5 8.1
8.7 8.1 7.7 7.3 7.1 7.1 7.1 7.3 7.7 8.1 8.7
Any input to put me in the write direction would be appreciated!

Sign in to comment.

Accepted Answer

Adit Alware
Adit Alware on 26 May 2022
Hi,
I understand that you want an easier way of calculating distance of a bunch of points on plane with the star point you have created.
Your method of using a for loop to calculate distance looks fine to me. However, you can perform vectorization for the same:
px = [randi([-5,5],100,1),zeros(100,1),randi([0,10],100,1)]; % will create 100 random points on the plane
plot3(px(:,1),px(:,2), px(:,3),'o','color','b') %will plot all the points on the plane
distance = sqrt(sum(((star1-px).^2),2)) % will calculate the distance of each random point on plane with star1
Here randi([-5,5],100,1) will generate 100 random integers in the range(-5,5).
Using plot3 you can plot the generated points on your 2-D plane as shown above. However in this approach, points are not in an ordered manner.
If you want to have ordered set of points through vectorization approach you may try :
px =[floor(-5:0.1:4.9)',zeros(100,1),floor(0:0.1:9.9)'];
After this you may plot the points accordingly.
For your convinience, you can reshape the output of distance in form of a 10X10 matrix by:
distance= reshape(distance,[10,10]);
Regards!
  1 Comment
philio63
philio63 on 26 May 2022
Hey thanks. I'll try this out.
I did get my code to work with loops, although it is inefficient, and at high resolutions could take many minutes to solve.
So I am grateful for the advice you have given.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!