0

I need to make a small program that draws three circles, a line between the first two, and then determines if the third touches or intersects the line. I have done everything but the last part. I am trying to use the points to determine if the area is 0, which would mean that the third point is, in fact, intersecting the line. Right? Or I could use another way. Technically the third circle can be within 3 pixels of the line. The problem is near the bottom at the hashtag. I would appreciate any help or suggestions that move this in another direction. Thank you.

import turtle

x1, y1 = eval(input("Enter coordinates for the first point x, y: "))
x2, y2 = eval(input("Enter coordinates for the second point x, y: "))
x3, y3 = eval(input("Enter coordinates for the third point x, y: "))

turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
turtle.circle(3)

turtle.penup()
turtle.goto(x2, y2)
turtle.pendown()
turtle.circle(3)

turtle.penup()
turtle.goto(x3, y3)
turtle.pendown()
turtle.circle(3)

turtle.penup()
turtle.color("red")
turtle.goto(x1, y1)
turtle.pendown()
turtle.goto(x2, y2)

a = (x1, y1)
c = (x3, y3)
#can't multiply sequence by non-int of type 'tuple'

area = (a * c) / 2    

if area == 0:
    print("Hit")
else:
    print("Miss")

3 Answers3

0

Ther center of 3rd circle is (x3,y3) and have a radius 3 and you are trying to determine any intersection with ([x1,y1],[x2,y2]) line segment.

If any point in the line is within the circle, then there is an intersection. Circle region formula is: (x-x3)^2 + (y-y3)^2 < 3^2 You should test for every point on the line whether this inequality holds and if any single point satisfies this condition, then you can conclude that the line and circle intersect. The first step would be to determine coordinate points of line segment (all points between [x1,y1],[x2,y2] points in a straight line) then you can try to test these points in a loop.

fatihk
  • 7,789
  • 1
  • 26
  • 48
0

You can calculate the area of the triangle by defining vectors from one vertex to the other two (adding a third constant coordinate to embed the plane in 3-dimensional space (so cross-products make sense)),

#pseudocode
b = (x2, y2, 1) - (x1, y1, 1) = (x2-x1, y2-y1, 0)
c = (x3, y3, 1) - (x1, y1, 1) = (x3-x1, y3-y1, 0)

then take the cross-product of these,

a = b cross c = (by*cz-bz*cy, bz*cx-bx*cz, bx*cy-by*cx)

then take the magnitude of this resulting vector which is the area of the parallelogram defined by the two vectors,

pa = |a| = ax^2 + ay^2 + az^2

then divide by two to get the area of the triangle (half of the parallelogram).

ta = pa/2

Source: http://en.wikipedia.org/wiki/Triangle_area#Using_vectors

luser droog
  • 18,988
  • 3
  • 53
  • 105
0

Am I wright? The position of the circles to each other does not matter?

Make a linear function from the line between the two center points. (ax+b=y) Where a is the gradient and b is the y-intersection.

To rotate a through 90° is easy. Inverse and negate a.

Find b of the second linear function. b'=y-a'*x . At once replace the x,y with the coordinates of your 3. circle point. Now you have a linear function which is rectangular to the old one and where the third circle point is part of.

Intersect the old linear function with the new one.

You'll get the lot point. You need to find out the distance between the 3. circle point and the lot point and whether it is greater than the radius.

You need there functions (JS):

function makelinear (x1,y1,x2,y2){
         var temp=x2-x1;
         if(temp==0)temp=0.00000000000001;//not clean but fast.
         var a=(y2-y1)/temp,
             b=y1-a*x1;
         return[a,b];
         }
function ninetydeg(a,b,x,y){
         var aout=1/a,
             bout=y+aout*x;
         return [aout,bout];
         }
function lineintersection(a1,b1,a2,b2){
         var temp=a1-a2;
         if(temp==0)temp=0.00000000000001;
         var x=(b2-b1)/temp,
             y=a1*x+b1;
         return[x,y];
         } 
function distance(x1,y1,x2,y2){
         var x=x1-x2,
             y=y1-y2;
         return(Math.sqrt(x*x+y*y));
         }

Sorry for to be complicate, I've found no other solution in short time. May be there is a vector solution.

B.F.
  • 477
  • 6
  • 9