Intersection of a triangle and a circle - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Intersection of a triangle and a circle (/thread-16337.html) |
Intersection of a triangle and a circle - Gira - Feb-23-2019 Determine whether a common point the two flat shapes: triangle, given the coordinates of its vertices and the circle with the given radius and center at the origin. I am from Russia and i cant get help in russian community help please if u understand what i whrite Thats what i can but its not always true from math import sqrt print("Введите координаты первой точки через пробел: ") x1, y1 = (int(i) for i in input().split()) print("Введите координаты второй точки через пробел: ") x2, y2 = (int(i) for i in input().split()) print("Введите координаты третьей точки через пробел: ") x3, y3 = (int(i) for i in input().split()) print("Введите радиус окружности: ") R = int(input()) rast1 = abs(x1*y2 - x2*y1) / sqrt((y1-y2)**2 + (x2-x1)**2) rast2 = abs(x1*y3 - x2*y1) / sqrt((y1-y3)**2 + (x3-x1)**2) rast3 = abs(x3*y2 - x2*y1) / sqrt((y3-y2)**2 + (x2-x3)**2) if sqrt(x1 ** 2 + y1 ** 2)<=R: print("YES") elif sqrt(x2 ** 2 + y2 ** 2)<=R: print("YES") elif sqrt(x3 ** 2 + y3 ** 2)<=R: print("YES") if rast1 <= R and ( abs(y1) < R or abs(y2) < R ) and (abs(x1) < R or abs(x2) < R): print("YES") if rast2 <= R and ( abs(y1) < R or abs(y3) < R ) and (abs(x1) < R or abs(x3) < R): print("YES") if rast3 <= R and ( abs(y3) < R or abs(y2) < R ) and (abs(x3) < R or abs(x2) < R): print("YES") else: print("NO")Translation from math import sqrt print ("Enter the coordinates of the first point separated by a space:") x1, y1 = (int (i) for i in input (). split ()) print ("Enter the coordinates of the second point separated by a space:") x2, y2 = (int (i) for i in input (). split ()) print ("Enter the coordinates of the third point separated by a space:") x3, y3 = (int (i) for i in input (). split ()) print ("Enter the radius of the circle:") R = int (input ()) rast1 = abs (x1 * y2 - x2 * y1) / sqrt ((y1-y2) ** 2 + (x2-x1) ** 2) rast2 = abs (x1 * y3 - x2 * y1) / sqrt ((y1-y3) ** 2 + (x3-x1) ** 2) rast3 = abs (x3 * y2 - x2 * y1) / sqrt ((y3-y2) ** 2 + (x2-x3) ** 2) if sqrt (x1 ** 2 + y1 ** 2) <= R: print ("YES") elif sqrt (x2 ** 2 + y2 ** 2) <= R: print ("YES") elif sqrt (x3 ** 2 + y3 ** 2) <= R: print ("YES") if rast1 <= R and (abs (y1) <R or abs (y2) <R) and (abs (x1) <R or abs (x2) <R): print ("YES") if rast2 <= R and (abs (y1) <R or abs (y3) <R) and (abs (x1) <R or abs (x3) <R): print ("YES") if rast3 <= R and (abs (y3) <R or abs (y2) <R) and (abs (x3) <R or abs (x2) <R): print ("YES") else: print ("NO") RE: Intersection of a triangle and a circle - heiner55 - May-18-2019 We could help better, if your program comments are in english. RE: Intersection of a triangle and a circle - perfringo - May-18-2019 My russian is becoming rusty, but my understanding is that user inputs are asked with following texts: "Enter the coordinates of the first/second/third point separated by a space" and "Enter the radius of the circle" RE: Intersection of a triangle and a circle - heiner55 - May-19-2019 Assuming the task is to determine if a triangle-shape and a circle-shape have at least one common point. I think that rast2 and rast3 are wrong: They should be: # shortest distance between origin and line given by two points rast1 = abs(x1*y2 - x2*y1) / sqrt((y1-y2)**2 + (x2-x1)**2) rast2 = abs(x1*y3 - x3*y1) / sqrt((y1-y3)**2 + (x3-x1)**2) rast3 = abs(x3*y2 - x3*y2) / sqrt((y3-y2)**2 + (x2-x3)**2) |