Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Line and Circle Intersection

Name: Anonymous 2011-03-16 13:16

Hello I could really use some from help from all the python programming wizards.
Create a Line and Circle intersection program.
1.Ask user to enter the slope and y-intercept of a line.
2.Ask the user to enter the radius of a circle.
3.Ask the user to enter the x and y values of the center of the circle.
4.Output the two intersection points between the line and circle
If there is not an intersection, output “No Solution.”
5.Ask the user to enter yes or no. If they enter yes, the  program is to restart. If they
enter no, the program is to terminate.
I just want to know were I should start.
Thanks

Name: Anonymous 2011-03-16 13:44

Start by asking the user to enter the slop and y-intercept of a line.

Name: Anonymous 2011-03-16 14:10

from math import sqrt

def input_values():
    def get_float(s):
        return float(raw_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)) / a  
    x2 = (-b - sqrt(D)) / a
    print ((x1, slope*x1 + y_intercept), (x2, slope*x2 + y_intercept))  

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

Name: Anonymous 2011-03-16 14:37

I just want to know were I should start.


$ vim prog.py
a#/usr/bin/env python
print "hello"
<ESC><C-Z>chmod +x prog.y
$ ./prog.py
hello
$ fg <CR>


here you go

Name: Anonymous 2011-03-16 14:38

>>4
Fuckity. [aa]  prompt looks so stylish and 1980

Name: Anonymous 2011-03-16 15:04

>>3
    x1 = (-b + sqrt(D)) / a  
    x2 = (-b - sqrt(D)) / a

Isn't that wrong?

Name: Anonymous 2011-03-16 16:44

>>6
No, I actually mean "b/2" by b. It so happened that it had all even quotients.

In other news, doing basic math turned out to be unexpectedly hard for me, so to hone my skills I intend to post solutions to all homework threads from now on, NO EXCEPTIONS.

Name: Anonymous 2011-03-16 17:01

>>3
from math import sqrt

Why bother with from? Does it make the program slower if you import the whole math module?

Name: Anonymous 2011-03-16 17:39

>>8
No.

Explicit imports are good for my mental health. When I revisit the code after a few months, I don't enjoy struggling to understand where the stuff it uses came from.

Name: Anonymous 2011-03-16 21:37

>>8
You think it really works that way? Imports the entirety of math, picks out sqrt and then discards the rest of math?

Name: Anonymous 2011-03-16 23:56

>>9
That's why you write import math. Then in a few months, you'll not have to struggle to understand anything, because every time you come across something that came from math, you'll immediately know it came from math because it starts with math!

Name: Anonymous 2011-03-17 21:15

This is so coincidental since we have this EXACT project due in class next Wednesday. What makes it even more coincidental is that the day that we don't have class this beautiful information pops up. I wonder if the professor would look here to see if this is where we got the answer? Nice

Name: Anonymous 2011-03-17 22:04

This is so coincidental since we have this EXACT project due in class next Wednesday. What makes it even more coincidental is that the day that we don't have class this beautiful information pops up. I wonder if the professor would look here to see if this is where we got the answer? I lol'd

Name: Anonymous 2011-03-17 22:17

This is so coincidental since we have this EXACT project due in class next Wednesday. What makes it even more coincidental is that the day that we don't have class this beautiful information pops up. I wonder if the professor would look here to see if this is where we got the answer? Not this shit again

Name: Anonymous 2011-03-17 22:19


            ∧_∧   / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
          ( ´∀`) < VIVA MEOWXICO! and ALBERTOO DEL RRRIO!
        /    |    \
       /       .|      ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
       / "⌒ヽ |.イ |
   __ |   .ノ | || |__
  .    ノく__========つ--
   _((_________\
    ̄ ̄ヽつ ̄ ̄ ̄ ̄ ̄ ̄ | | ̄

Name: Anonymous 2011-03-18 1:52

>>14
nice.

Name: Anonymous 2011-03-18 1:52

>>14
I lol'd

Name: Anonymous 2011-03-18 6:24

>>11
you'll immediately know it came from math because it starts with math!
I'm willing to surrender the ability to know it immediately and not one second later, for brevity.

Name: Anonymous 2011-03-18 10:38

from math import sqrt; sqrt; sqrt
import math; math.sqrt; math.sqrt


Yeah they're exactly the same.

Name: Anonymous 2011-03-18 10:53

>>10

It doesn't discard the rest of the module, it just doesn't become visible for you at that moment. Python modules initialize once.

Name: Anonymous 2011-03-18 11:38

LISP

Name: Anonymous 2011-03-18 12:24

>>19

from math import sqrt; sqrt; sqrt; sqrt;
import math; math.sqrt; math.sqrt; math
.sqrt; math.sqrt

Name: Anonymous 2011-03-18 13:24

>>22
fuck you faggot

Name: Anonymous 2011-03-18 14:17

>>22
Well. The fuck. Done. You win the “Best Autist To Ruin Humour By Stating The Fucking Obvious Like A Mindless Fucking Retard” award.

Name: Anonymous 2011-03-18 16:48

>>24
Thank you.

Name: Anonymous 2011-03-18 17:24

I had to say something. I look forward to seeing what idiots actually turn this coding in. At least they tried I guess.

Name: Anonymous 2011-03-18 17:51

>>24
* Atist

Name: Anonymous 2011-03-18 22:38

>>27
* AT&Tist

Don't change these.
Name: Email:
Entire Thread Thread List