(Optimization) How to quickly get coordinates (coordinates are integer) of all possible points inside a specific cylinder?

조회 수: 4 (최근 30일)
Let's say I know coordinates of the center points of bottom surface and top surface; radius (R) and height (H) of cylinder. I need to write down all points which are inside the cylinder and also have integer coordinates.
The calculation speed could not be too slow since I have lots of calculation to run and need my simulation work down in rational time!
_____________________________________________________________________________
Roger Stafford has given a right method. Is any one can optimize the method for improving calculation speed? Since I need to do lots of that calculation. _____________________________________________________________________________

채택된 답변

Roger Stafford
Roger Stafford 2014년 10월 4일
편집: Roger Stafford 2014년 10월 4일
The hard part of your question is formulating the equations of the cylinder's bounding surfaces.
If P1 = [x1,y1,z1] and P2 = [x2,y2,z2] are the center points you referred to, and R is the cylinder's radius, then the equation of any point P = [x,y,z] on the infinite cylinder's surface is:
dot(P-P1,P-P1)-dot(P-P1,P2-P1)^2/dot(P2-P1,P2-P1) = R^2
The equation of the infinite plane of the end containing P1 is:
dot(P-P1,P2-P1) = 0
and for the other end
dot(P-P2,P2-P1) = 0.
Nested for-loops can find all the points within the cylinder with integer-valued coordinates:
in = zeros(ceil(pi*(R+1)^2*(norm(P2-P1)+2)),3);
k = 0;
H2 = dot(P2-P1,P2-P1);
for x = floor(-R+min(x1,x2)):ceil(R+max(x1,x2))
for y = floor(-R+min(y1,y2)):ceil(R+max(y1,y2))
for z = floor(-R+min(z1,z2)):ceil(R+max(z1,z2))
P = [x,y,z];
if dot(P-P1,P-P1)-dot(P-P1,P2-P1)^2/H2<=R^2 & ...
dot(P-P1,P2-P1)>=0 & dot(P-P2,P2-P1)<=0
k = k + 1;
in(k,:) = P;
end
end
end
end
in(k+1:end,:) = [];
[Corrected]
  댓글 수: 3
yogesh jain
yogesh jain 2015년 12월 9일
Thank you very much Yi , this code helped me very much for different purpose . :)
yogesh jain
yogesh jain 2016년 2월 20일
If I want to modify this for cone then what parameters should I change ?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 10월 4일
Why are the (x,y) coordinates at some z level any different than those at the end caps? Why can't you just use repmat() to copy them?
  댓글 수: 5
Image Analyst
Image Analyst 2014년 10월 5일
Possibly the easiest way is to just rotate the cap so that it is parallel to the x-y plane (it's all in 1 z level), then use repmat(). Then rotate back.
Yi
Yi 2014년 10월 5일
Yeah, that would definitely be much easier for calculation. But how could we know which point is gonna have integer coordinates after rotating back? since in the rotation, a point with integer coordinates might no longer have integer coordinates. My point is that if we are not calculating points with integer coordinates any more, how do we know all possible points are calculated and which point should be calculate?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by