-1

I am stuck at a problem. I am trying to solve it for 2 days. But I am clueless. It can be something simple but just i am clueless at the moment & any help is appreciated.

I want to make a line from A(x1, y1) to B(x2, y2) of fixed length.

A.............B
A....B........
A.............    B

I have made a mobile cricket scorer. In reports it builds a wagon wheel. As it is used from mobile the human touch is not very accurate and people do not always touch the boundary line (dark orange circle in image below).

But when I am generating a report in PHP, I must draw a line from fixed starting point to the boundary line in the direction where they touched.

Right now my WAGON WHEEL looks like this.

enter image description here

I have tried many ways to accomplish this but all failed. Now my next idea is to draw a line of fixed length from fixed starting point (batting side) to the touch co-ordinates but of fixed length so that the line always end at boundary line no matter if the users' touch is not accurate.

I can calculate the distance between starting point and touch point using this in php

    $lineLength = round( sqrt (pow(($wicketX-$x),2) + pow(($wicketY-$y),2)), 2);

But I dont know how to further adjust by touch co-ordinates to required length

Please please please help.

P.S. This question looked something similar but i figure out. My maths is week Calculate a point along the line A-B at a given distance from A

Community
  • 1
  • 1
Sallu
  • 479
  • 6
  • 17
  • If A is the center, AB is not "of fixed length". –  Aug 03 '15 at 09:20
  • May be I did not explain the question correctly but I couldnt get the right answer. I wanted to find a new point in the same line. I finally used this formula to found the point 3. `code` $x3 = $x2 + ($x2 - $x1) / $lineLength * $newLineLength; $y3 = $y2 + ($y2 - $y1) / $lineLength * $newLineLength; – Sallu Aug 03 '15 at 12:56
  • HOW Can I please close this question. – Sallu Aug 03 '15 at 13:00
  • Don't you like my answer ? –  Aug 03 '15 at 13:04
  • Actually I do not have a circle. The ground is in the shape of eclipse. So I dont want to find a point of intersection of a line to the circle. I wanted to know any point on the line at distance X . Let the line be from point (x1, y1) to (x2, y2). I want to know any point on this line before or after (x2, y2). If you see the solution that I have posted in above comment, using the $newLineLength I can calculate. – Sallu Aug 03 '15 at 16:55
  • Thanks for your time taken to write a detailed reply. I am sure it will help someone else who is looking to find the intersection with a circle. It was even a teaching thing for me. Thank you so much for your valued effort. I am extremely grateful. – Sallu Aug 03 '15 at 16:58

1 Answers1

0

I understand that you want to find the intersection of the line through the points A and B with a circle. A needn't be the center.

Let the center of the circle have coordinates (Xc, Yc) and the radius be R.

Any point along AB can be written as

X = Xa + t (Xb - Xa) = Xc + (Xa - Xc) + t (Xb - Xa) = Xc + Xca + t Xab
Y = Ya + t (Yb - Ya) = Yc + (Ya - Yc) + t (Yb - Ya) = Yc + Yca + t Yab

You plug that in the equation of the circle

(X - Xc)² + (Y - Yc)² - R² = (Xca + t Xab)² + (Yca + t Yab)² - R² 
                           = (Xab² + Yab²) t² + 2 (Xca Xab + Yca Yab) t + Xca² + Yca² - R²
                           = 0

This is a second degree equation that you need to solve for t. Use the solution with positive t, it is on the side of B. From t, compute (X, Y).


Note that if A is the center, the equation trivially reduces to

(Xab² + Yab²) t² = R²