Last active
August 29, 2015 14:11
-
-
Save sdex/5a596d191877228b668a to your computer and use it in GitHub Desktop.
Determining point position
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://pp.vk.me/c625329/v625329501/1e47/wWQSR_4v0bA.jpg | |
import math | |
def spo(color, x, y): | |
result = { | |
'red': lambda x,y: x + 10/(x+y), | |
'blue': lambda x,y: x + 1/y, | |
'yellow': lambda x,y: x/(y-x), | |
'white' : lambda x,y: x * y | |
}[color](x, y) | |
print('Color: ' + color) | |
print('SPO = ' + str(result)) | |
def sign(n): | |
if n == 0: | |
return 0; | |
return abs(n)/n | |
def isInsideTriangle(xa, ya, xb, yb, xc, yc, xp, yp): | |
a = (xa-xp)*(yb-yp)-(xb-xp)*(ya-yp) | |
b = (xb-xp)*(yc-yp)-(xc - xp)*(yb-yp) | |
c = (xc-xp)*(ya-yp)-(xa - xp)*(yc-yp) | |
return sign(a)==sign(b) and sign(b)==sign(c) | |
print("Enter radius") | |
r = float(input("R = ")) | |
# circle center | |
x0 = r | |
y0 = -r | |
print("Enter point coordinates") | |
x = float(input("x = ")) | |
y = float(input("y = ")) | |
if (x > 0 and y > 0) or (x < 0 and y > 0) or (x < 0 and y < 0): | |
spo('yellow', x, y) | |
else: | |
r_xy = math.sqrt((x0-x)*(x0-x)+(y0-y)*(y0-y)) | |
if r_xy <= r: | |
print("Point belongs to the circle") | |
if x >= r: | |
if y < -r: | |
#print("sector 2") # bottom right | |
if isInsideTriangle(r, -r, 2*r, -r, r, -2*r, x, y): | |
spo('white', x, y) | |
else: | |
spo('blue', x, y) | |
else: | |
#print("sector 1") # top right | |
if isInsideTriangle(r, -r, r, 0, 2*r, -r, x, y): | |
spo('white', x, y) | |
else: | |
spo('red', x, y) | |
else: | |
if y < -r: | |
#print("sector 3") # bottom left | |
if isInsideTriangle(0, -r, r, -r, r, -2*r, x, y): | |
spo('white', x, y) | |
else: | |
spo('red', x, y) | |
else: | |
#print("sector 0") # top left | |
if isInsideTriangle(0, -r, r, 0, r, -r, x, y): | |
spo('white', x, y) | |
else: | |
spo('blue', x, y) | |
else: | |
print("Point does not belongs to the circle") | |
if x > r and y < -r: | |
spo('yellow', x, y) | |
else: | |
spo('white', x, y) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment