필터 지우기
필터 지우기

Intersection between a circle and a line.

조회 수: 10 (최근 30일)
Shrishti Yadav
Shrishti Yadav 2021년 11월 3일
편집: Matt J 2021년 11월 4일
Goal: To get the coordinates of line from every x-y point in the plane intersecting a circle with a given radius.
I am not sure if it is doing that. I was using tan before but I think tan2 works better.I wanted to use equations to solve y= mx and the circle equation but that seemed too complicated. Please let me know if you know of an easier way of doing this.
%calculates the intersection points of a line and a circle
x = -a:b;
y = (-a:b).';
u = numel(x);
Z = zeros(u*u,2);
theta = atan2((y-cy) ,(x-cx)); %cx,cy- center of the circle.
Zx = cx + r*cos(theta);
Zy = cy + r*sin(theta);
Z = [Zx(:) Zy(:)];
  댓글 수: 2
John D'Errico
John D'Errico 2021년 11월 4일
What you are doing is far more complex. Anyway, you CANNOT compute the coordinates of every point that falls inside a region, since there are infinitely many such points. Unless you are asking how to find the points on the line that intersect the perimeter of the circle.
So what is your goal? To get a list of points on the line inside the circle? Or just the two points where the line crosses the perimeter?
Shrishti Yadav
Shrishti Yadav 2021년 11월 4일
To find the coordinates at the intersection of a line from any given (x,y) location to the circle. There are two points for a y = mx intersection with a circle equation.
Thats the goal. In the code I am just using multiple different x,y locations. E.g if x= 1:5 and y =1:5, the total number of unique x,y locations are 25. So for every one of those 25 locations if we draw a line that goes through the center of a circle, it will intersect the circle at two points. Those two points will happen at a coordinates and that is what the code outputs.

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

답변 (1개)

Matt J
Matt J 2021년 11월 4일
편집: Matt J 2021년 11월 4일
One way,
[dx,dy]= ndgrid( (-a:b)-cx , (-a:b)-cy );
factor=r./sqrt(dx.^2+dy.^2);
Z=[dx(:),dy(:)].*factor(:)+[cx,cy];

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by