Author Topic: A programming challenge all up in your face.  (Read 14429 times)

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
A programming challenge all up in your face.
« on: 29 January 2006, 21:08 »
Ladies and gentlemen, it's time for another programming challenge. This one deals with circles and lines, so some math will be required.

A picture is worth a thousand words, so I'll start with an illustration I drew.



Your mission: Find the length of the bold curve.

Input: Your program should read a file called "INPUT," which is in the following format:

x_i
y_i
x_f
y_f
n
x_1 y_1 r_1
x_2 y_2 r_2
...
x_n y_n r_n

Example:
1
10
10
1
1
5 5 4

(x_i denotes the x-coordinate of the BEGINNING of the curve; y_i denotes of y-coordinate of the BEGINNING of the curve; x_f denotes the x-coordinate of the END of the curve; y_f denotes the y-coordinate of the END of the curve; n denotes the number of circles there are; x_j denotes the x-coordinate of the center of circle j; y_j denotes the y-coordinate of the center of circle j; r_j denotes the radius of circle j.)

None of the circles touch or intersect, and all the figures belong to the first quadrant/the two axes.

Output: your program should output the length of the curve to a file called "OUTPUT."

The following math things may prove useful.

The equation of a line with slope a and y-intercept b is y = ax + b.

The slope can be found by taking two points which belong to the line, (x_1, y_1) and (x_2, y_2), and performing the following operation:
Code: [Select]

    y_2 - y_1
a = ---------
    x_2 - x_1

The distance between two points with coordiantes (x_1, y_1) and (x_2, y_2) equals sqrt((x_2 - x_1)^2 + (y_2 - y_1)^2)

The standard equation of a circle of radius r with center at (x_0, y_o) is::
Code: [Select]

(x - x_0)^2 + (y - y_0)^2 = r^2

Note that any positive number has TWO square roots. E.g. sqrt(4) = {-2, 2}, not just 2.

A quadratic equation is an equation in the form ax^2 + bx + c = 0. The solution is this:
Code: [Select]

       -b - sqrt(b^2 - 4ac)
x_1 =  --------------------
                 2a

       -b + sqrt(b^2 - 4ac)
x_2 =  --------------------
                 2a

Note that if b^2 - 4ac > 0, then there are two solutions; if b^2 - 4ac = 0, there is one solution; if b^2 - 4ac < 0, then there are no real solutions. (The expression b^2 - 4ac is called the discriminate, and is usually represented by the capital letter D.)

When making algebraic manipulations, don't forget about the domain! For example, suppose you have to solve the following equation:
Code: [Select]

(sqrt(x))^2 = -15

The answer is "no solution"--NOT -15. The square root function has the domain [0, +inf)!

The formula for the length of an arc is:
Code: [Select]

s = Ar

Where A is the measure of the angle in radians, and r is the radius of the circle.

In addition, I suggest looking up the Law of Cosines (or, alternatively, Pythagorean
« Last Edit: 15 March 2006, 00:14 by TheQuirk »

Orethrius

  • Member
  • **
  • Posts: 1,783
  • Kudos: 982
Re: A programming challenge all up in your face.
« Reply #1 on: 30 January 2006, 00:19 »
[offtopic]
Quote from: TheQuirk
Code: [Select]
(sqrt(x))^2 = -15
The answer is "no solution"--NOT -15. The square root function has the domain (0, +inf)!

Not intending to be a nit or anything, but isn't (√x)^2 == x, where x ≥ 0?  Sorry for the tangent, I'm just trying to see if I correctly remember my Calculus classes.

[/offtopic]

Proudly posted from a Gentoo Linux system.

Quote from: Calum
even if you're renting you've got more rights than if you're using windows.

System Vitals

H_TeXMeX_H

  • Member
  • **
  • Posts: 1,988
  • Kudos: 494
    • http://draconishinobi.50webs.com/
Re: A programming challenge all up in your face.
« Reply #2 on: 30 January 2006, 01:17 »
[OFFTOPIC]The square root of anything squared is the absolute value of that something ... meaning a positive number[/OFFTOPIC]

Pathos

  • Member
  • **
  • Posts: 518
  • Kudos: 416
Re: A programming challenge all up in your face.
« Reply #3 on: 30 January 2006, 09:52 »
Quote from: Orethrius
[offtopic]


Not intending to be a nit or anything, but isn't (√x)^2 == x, where x ≥ 0?  Sorry for the tangent, I'm just trying to see if I correctly remember my Calculus classes.

[/offtopic]

If you use complex numbers it works, if you algebraically cancel the two operations out it works. If you attempt the find the Real root your screwed.

Pathos

  • Member
  • **
  • Posts: 518
  • Kudos: 416
Re: A programming challenge all up in your face.
« Reply #4 on: 30 January 2006, 09:57 »
Will the circles ever overlap the end points of the line?

Orethrius

  • Member
  • **
  • Posts: 1,783
  • Kudos: 982
Re: A programming challenge all up in your face.
« Reply #5 on: 30 January 2006, 18:47 »
Quote from: Pathos
If you use complex numbers it works, if you algebraically cancel the two operations out it works. If you attempt the find the Real root your screwed.
You have already lost.
The correct answer is always "42."

Having said that, 42.   QED. :D

Proudly posted from a Gentoo Linux system.

Quote from: Calum
even if you're renting you've got more rights than if you're using windows.

System Vitals

piratePenguin

  • VIP
  • Member
  • ***
  • Posts: 3,027
  • Kudos: 775
    • http://piratepenguin.is-a-geek.com/~declan/
Re: A programming challenge all up in your face.
« Reply #6 on: 31 January 2006, 02:25 »
http://illhostit.com/files/3913162470455711/curve_q.png
Whenever the curve is going around a circle it can go around it clockwise or anti-clockwise, does it always go clockwise or should it go for the shortest route?
"What you share with the world is what it keeps of you."
 - Noah And The Whale: Give a little love



a poem by my computer, Macintosh Vigilante
Macintosh amends a damned around the requested typewriter. Macintosh urges a scarce design. Macintosh postulates an autobiography. Macintosh tolls the solo variant. Why does a winter audience delay macintosh? The maker tosses macintosh. Beneath female suffers a double scum. How will a rat cube the heavier cricket? Macintosh calls a method. Can macintosh nest opposite the headache? Macintosh ties the wrong fairy. When can macintosh stem the land gang? Female aborts underneath macintosh. Inside macintosh waffles female. Next to macintosh worries a well.

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
Re: A programming challenge all up in your face.
« Reply #7 on: 31 January 2006, 16:32 »
Shortest route, and no, the circles don't overlap.

About the (sqrt(x))^2 = x thing: for x >= 0, yes, (sqrt(x))^2 = x is correct. What a lot of people do, though, is forget that the square root function has a domain of [0; inf) and solve things like (sqrt(x))^2 = -1 in the real number system.

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
Re: A programming challenge all up in your face.
« Reply #8 on: 20 September 2006, 08:09 »
Let's take this thing apart piece  piece!

Before we write any code, we have to answer a few questions:

a) How do we represent the line mathematically?
b) How do we represent the circles mathematically?
c) How do we check whether the line touches/intersects a given circle?
d) If the line intersects a given circle, what is the arc length which is "created"?

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: A programming challenge all up in your face.
« Reply #9 on: 20 September 2006, 08:49 »
a)
slope = (y2-y1)/(x2-x1);
b = y1 - (slope * x1);

the line's equation is then:
y = b + (slope * x);
or maybe:
y = y1-m(x+x1);
or if you prefer:
y = y1 - (((y2-y1)/(x2-x1)) * (x+x1));

Even though the order of operations covers most of the math, I still use parentheses, for clarity.

So is that enough?  Or do we need to build some kind of array of points that satisfy the equation?

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
Re: A programming challenge all up in your face.
« Reply #10 on: 20 September 2006, 08:52 »
Should be enough.

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: A programming challenge all up in your face.
« Reply #11 on: 20 September 2006, 10:23 »
Quote from: TheQuirk
Should be enough.
You're sooooooo helpful.

Okay,
(b)

how about:
y = q + sqrt(r^2 - (x-p)^2);

where p,q is the center, and r is the radius

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
Re: A programming challenge all up in your face.
« Reply #12 on: 20 September 2006, 11:40 »
Well, what else was I supposed to say? You don't need an array because there are infinitely many possibilities of locations and radii of circles, so you can't just have an array with all the, say, points with integer coordinates and check whether they belong to the circles or not.

Anyway!

Quote
y = q + sqrt(r^2 - (x-p)^2);

Almost. That only gives you half a circle. This is what it should be:
Quote
y = q +- sqrt(r^2 - (x-p)^2);

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: A programming challenge all up in your face.
« Reply #13 on: 21 September 2006, 00:53 »
c) set the two y= equations above equal and solve?  But what variable do we solve for?  There are 7 unknowns, and that's for only one circle.

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
Re: A programming challenge all up in your face.
« Reply #14 on: 21 September 2006, 00:57 »
What are the other six unknowns?