how do i find chord's length?

So i need to do a project where i have following information in coordinate plane:
1. circle's center coordinates(which program's user declares)
2. circle's radius
3. number of straight lines (which user also declares)
4. those straight lines coordinates

And I need to find which of those straight lines are chords and what is their length, in that place where it crosses the circle.

P.S. I need to use static array.
Last edited on
Looks reasonable (apart from the "straight lines coordinates" bit, which I assume is your paraphrasing).

Which part are you having problems with and how much code have you written?


A straight line is a chord of a circle if it cuts it twice. Substitute the equation of the straight line into that of the circle and you will end up with a quadratic equation for (e.g.) the x coordinates. The discriminant of that equation will tell you if you have two real roots.
well you see I started writing code, was half-way done, but then realized that everything I coded was wrong and had to remake it, but I have no idea how to do it now :/
So I have problems everywhere, I mean I only have an idea of how to find chord's length.
Last edited on
Before you try writing any code, are you certain you understand the basic maths needed to solve the problem on paper?

Because if you don't even understand what it is your code has do, you can't possibly write the code to do it.
Another suggestion:
If it is a chord it will also have a perpendicular distance to the center of the circle less than (or equal to) the radius.
https://www.intmath.com/plane-analytic-geometry/perpendicular-distance-point-line.php

and, the length of that chord (L) can be found by using Pythagoras on the radius (R) and the perpendicular distance (d).
L = 2 * sqrt(R^2 - d^2)
DanielJank wrote:
I mean I only have an idea of how to find chord's length.


Well, if that's so, then you would presumably know how to solve the problem.


Mathematically:
- write down the equation of a circle, centred (x0, y0), radius R:
(x-x0)^2 + (y-y0)^2 = R^2

- substitute the equation of a straight line for either x or y; excluding the "edge" case of a vertical line (which you would have to treat separately) this could be
y = mx + c

- This will give you a quadratic for x (you'll have to work out the coefficients):
ax^2 + bx + c = 0
This will have two solutions (i.e. the line will be a chord) if the discriminant b^2-4ac is greater than 0. The two solutions will then be the two x coordinates of the ends of the chord. Substitute these in the straight-line equation to get the y coordinates. Finally, find the distance between the two points to get the length of chord.

If the straight lines are given in other ways then there are other ways to answer the question ... but you haven't told us enough about that.


EDIT: just seen @againtry's approach, which is definitely very good. It depends how you choose to specify your straight lines ... so we need to see your code and intended route to a solution.
Last edited on
Yes, we need more information so that the most appropriate trigonometric tools can be applied perhaps even vectors :)
(Based on @againtry's link)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
using namespace std;

bool isChord( double a, double b, double c, double x0, double y0, double R, double &length )
{
   double temp = a * x0 + b * y0 + c;                 
   double dsq = temp * temp / ( a * a + b * b );      // (square of) distance from centre to chord
   if ( dsq > R * R ) return false;                   // not a chord
   length = 2.0 * sqrt( R * R - dsq );                // by Pythagoras
   return true;
}


int main()
{
   double x0, y0, R, a, b, c, length;
   cout << "Circle, centre (x0,y0), radius R.   Enter x0  y0  R: ";   cin >> x0 >> y0 >> R;
   cout << "Line    a x + b y + c = 0.          Enter a   b   c: ";   cin >> a  >> b  >> c;

   if ( isChord( a, b, c, x0, y0, R, length ) ) cout << "Line segment is a chord, with length " << length << '\n';
   else                                         cout << "Line misses the circle\n";
}


Circle, centre (x0,y0), radius R.   Enter x0  y0  R: 3 3 2
Line    a x + b y + c = 0.          Enter a   b   c: 1 -1 0
Line segment is a chord, with length 4


Circle, centre (x0,y0), radius R.   Enter x0  y0  R: 3 3 2
Line    a x + b y + c = 0.          Enter a   b   c: 10 -1 0
Line misses the circle
Last edited on
Topic archived. No new replies allowed.