SRL Archive - Python/Perl Help and Tutorials

Line and Circle Interception


VandyTech

2011-03-16 15:34:02

I' m making a line and circle interception program, and it's not working. What am I doing wrong? Thanks.:fiery:
from math import sqrt

def input_values():
def get_float(s):
return float(input('Enter %s: ' % s))
return (get_float('slope'), get_float('y_intercept'),
get_float('radius'), get_float('center x'), get_float('center y'))

def calc_intercection(slope, y_intercept, radius, center_x, center_y):
a = 1 + slope**2
b = -center_x + slope, y_intercept - slope*center_y
c = center_x**2 + center_y**2 + y_intercept**2 - 2*y_intercept*center_y - radius*radius
D = b**2 - a*c
if D < 0:
print('No Solutions')
return
x1 = (-b + sqrt(D)) /2*a
x2 = (-b - sqrt(D)) /2*a
print((x1, slope*x1 + y_intercept), (x2, slope*x2 + y_intercept))


while True:
calc_intercection(input_values())
if input('Enter "Y" to continue: ') != 'Y': break

Floor66

2011-03-16 15:35:46

For sheer readability, you might want to wrap your code in
 tags, it will probably help whoever takes a look at it ;)

Nava2

2011-03-16 15:41:45

from math import sqrt

def input_values():
def get_float(s):
return float(input('Enter %s: ' % s))
return (get_float('slope'), get_float('y_intercept'),
get_float('radius'), get_float('center x'), get_float('center y'))

def calc_intercection(slope, y_intercept, radius, center_x, center_y):
a = 1 + slope**2
b = -center_x + slope, y_intercept - slope*center_y
c = center_x**2 + center_y**2 + y_intercept**2 - 2*y_intercept*center_y - radius*radius
D = b**2 - a*c
if D < 0:
print('No Solutions')
return
x1 = (-b + sqrt(D)) /2*a
x2 = (-b - sqrt(D)) /2*a
print((x1, slope*x1 + y_intercept), (x2, slope*x2 + y_intercept))


while True:
calc_intercection(input_values())
if input('Enter "Y" to continue: ') != 'Y': break

Wrapped in the proper tags..

VandyTech

2011-03-16 21:27:13

SO….. can any one tell me why I'm getting a error.

Traceback (most recent call last):
File "C:\Users\Sylvia\Documents\Python Saves\LCI Program.py", line 23, in <module>
calc_intersection(input_values())
TypeError: calc_intersection() takes exactly 6 arguments (1 given)

Gu3st...

2011-03-16 22:30:57

I'm not a python programmer, but
"return (get_float('slope'), get_float('y_intercept'), get_float('radius'), get_float('center x'), get_float('center y'))" looks like 1 argument to me… tried removing one nest of parentheses?

i luffs yeww

2011-03-16 22:41:38

That's returning a tuple, and taking off the parentheses will make the same exact tuple.

The problem is you're passing a single argument, as a tuple is a single thing (it just contains multiple things).

And then after that, you have b set to a tuple as well, so you need to change that, because you can't do tuple**2.