简体   繁体   中英

Problems finding the intersecting point of circle

Recently I posted another thread and the answers were really helpful. From this reference I tried to write the code for two intersecting circle.

  1. I think I did something wrong with my code and the result it's giving me the mid point of the connecting line of the intersection points. I have no clue what's wrong with my code.

  2. I'm finding it difficult to handle the loop. I need the actual length of the vectors but if I wrote it to the loop, for i+1 it is showing me the error (which is very natural as I don't find any '<' operator :(()

Here's my code -

Xs  = [150,130];% x coordinates of center of circles
Ys = [100,250];% y coordinates of center of circles
Rs = [100,70];% radius of circles

C=Imread('spinks_map.png');
figure, Imshow(C)
hold on;

simple_draw_circle( Xs, Ys, Rs);


for i=1:length(Xs)-1
    dsq(i) = ((Xs(i+1)- Xs(i))^2) + ((Ys(i+1) - Ys(i))^2);
    K(i)= (sqrt((((Rs(i)+Rs(i+1))^2)-dsq(i))-(dsq(i)-(Rs(i)-Rs(i+1))^2)))/4;
    x(i) = ((Xs(i+1)+Xs(i))/2) + ((Xs(i+1)-Xs(i))*(Rs(i)^2-Rs(i+1)^2)/dsq(i))/2 - 2*(Ys(i+1)-Ys(i))*(K(i)/dsq(i));
    y(i) = ((Ys(i+1)+Ys(i))/2) + ((Ys(i+1)-Ys(i))*(Rs(i)^2-Rs(i+1)^2)/dsq(i))/2 +(-2)*(Xs(i+1)-Xs(i))*(K(i)/dsq(i));
end

scatter(x(1),y(1),'filled');

You have an error in your use of Heron's formula. Look at the link you posted that gives the calculation for K. There should be a product between the two terms inside the square root, not a difference:

K(i)= (sqrt((((Rs(i)+Rs(i+1))^2)-dsq(i))*(dsq(i)-(Rs(i)-Rs(i+1))^2)))/4;

Also, with regards to your second question. I've found it to be a best practice for me if I explicitly declare order of operations when using an expression with ":".

For your loop, using the appropriate choice of either

for i=(1:length(Xs))-1

or

for i=1:(length(Xs)-1)

can cut down on unexpected behaviors during indexing. This was the issue as I understood it from your post. If it was something else, then post the error text that Matlab spits up and I'll take a look at it.

There is a "<" operator in Matlab. If you're trying to use it in the style of the C equivalent for loop:

for(iter=0;iter++,iter<2){
     something();
}

then look into the syntax for the Matlab while loop.

Best of luck,

Andrew

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM