#arc_2points_find_center_00d
#https://usethinkscript.com/threads/kinematic-projectile-math-based-plot.15681/
#Kinematic Projectile math-based plot
#Human Jun 2, 2023
#
#I would like to create an indicator that uses kinematic projectile math based on two points. I imagine this could be like an anchored VWAP where you specify the starting point. In this case, I would like to use a line from a bottom and a top which are manually selected points. Then I would like to plot an arch, using projectile math, like throwing a ball from the end of the line defined by the two selected (manually called out) points. I hope I described that well enough. I imagine I would replace the coefficient used for the effect of gravity with a coefficient I could adjust to fit the particular characteristics of the stock. I would also input a date or a price level that would stop the plot. (Like the ball stopping on the ground.) Thank you for your help.
#----------------
# find center from python code
#https://www.geeksforgeeks.org/equation-of-circle-when-three-points-on-the-circle-are-given/
#Equation of circle when three points on the circle are given
#gyanendra371
#Given three coordinates that lie on a circle, (x1, y1), (x2, y2), and (x3, y3). The task is to find the equation of the circle and then print the centre and the radius of the circle.
#Equation of circle in general form is x² + y² + 2gx + 2fy + c = 0 and in radius form is (x – h)² + (y -k)² = r², where (h, k) is the centre of the circle and r is the radius.
#Examples:
#Input: x1 = 1, y1 = 0, x2 = -1, y2 = 0, x3 = 0, y3 = 1
#Output:
#Centre = (0, 0)
#Radius = 1
#The equation of the circle is x2 + y2 = 1.
#Input: x1 = 1, y1 = -6, x2 = 2, y2 = 1, x3 = 5, y3 = 2
#Output:
#Centre = (5, -3)
#Radius = 5
#Equation of the circle is x2 + y2 -10x + 6y + 9 = 0
#Recommended: Please try your approach on {IDE} first, before moving on to the solution.
#Approach: As we know all three-point lie on the circle, so they will satisfy the equation of a circle and by putting them in the general equation we get three equations with three variables g, f, and c, and by further solving we can get the values. We can derive the formula to obtain the value of g, f, and c as:
#Putting coordinates in eqn of circle, we get:
#x12 + y12 + 2gx1 + 2fy1 + c = 0 – (1)
#x22 + y22 + 2gx2 + 2fy2 + c = 0 – (2)
#x32 + y32 + 2gx3 + 2fy3 + c = 0 – (3)
#From (1) we get, 2gx1 = -x12 – y12 – 2fy1 – c – (4)
#From (1) we get, c = -x12 – y12 – 2gx1 – 2fy1 – (5)
#From (3) we get, 2fy3 = -x32 – y32 – 2gx3 – c – (6)
#Subtracting eqn (2) from eqn (1) we get,
#2g( x1 – x2 ) = ( x22 -x12 ) + ( y22 – y12 ) + 2f( y2 – y1 ) – (A)
#Now putting eqn (5) in (6) we get,
#2fy3 = -x32 – y32 – 2gx3 + x12 + y12 + 2gx1 + 2fy1 – (7)
#Now putting value of 2g from eqn (A) in (7) we get,
#2f = ( ( x12 – x32 )( x1 – x2 ) +( y12 – y32 )( x1 – x2 ) + ( x22 – x12 )( x1 – x3 ) + ( y22 – y12 )( x1 – x3 ) ) / ( y3 – y1 )( x1 – x2 ) – ( y2 – y1 )( x1 – x3 )
#Similarly we can obtain the values of 2g :
#2g = ( ( x12 – x32 )( y1 – x2 ) +( y12 – y32 )( y1 – y2 ) + ( x22 – x12 )( y1 – y3) + ( y22 – y12 )( y1 – y3 ) ) / ( x3 -x1 )( y1 – y2 ) – ( x2 – x1 )( y1 – y3 )
#Putting 2g and 2f in eqn (5) we get the value of c and know we had the equation of circle as x2 + y2 + 2gx + 2fy + c = 0
#Below is the implementation of the above approach:
## Python3 implementation of the approach
#from math import sqrt
# Function to find the circle on
# which the given three points lie
#=====================================
#def findCircle(x1, y1, x2, y2, x3, y3) :
# x12 = x1 - x2;
# x13 = x1 - x3;
# y12 = y1 - y2;
# y13 = y1 - y3;
# y31 = y3 - y1;
# y21 = y2 - y1;
# x31 = x3 - x1;
# x21 = x2 - x1;
# # x1^2 - x3^2
# sx13 = pow(x1, 2) - pow(x3, 2);
# # y1^2 - y3^2
# sy13 = pow(y1, 2) - pow(y3, 2);
# sx21 = pow(x2, 2) - pow(x1, 2);
# sy21 = pow(y2, 2) - pow(y1, 2);
# f = (((sx13) * (x12) + (sy13) *
# (x12) + (sx21) * (x13) +
# (sy21) * (x13)) // (2 *
# ((y31) * (x12) - (y21) * (x13))));
# g = (((sx13) * (y12) + (sy13) * (y12) + (sx21) * (y13) + (sy21) * (y13)) // (2 * ((x31) * (y12) - (x21) * (y13))));
# c = (-pow(x1, 2) - pow(y1, 2) - 2 * g * x1 - 2 * f * y1);
# # eqn of circle be x^2 + y^2 + 2*g*x + 2*f*y + c = 0
# # where centre is (h = -g, k = -f) and
# # radius r as r^2 = h^2 + k^2 - c
# h = -g;
# k = -f;
# sqr_of_r = h * h + k * k - c;
# # r is the radius
# r = round(sqrt(sqr_of_r), 5);
# print("Centre = (", h, ", ", k, ")");
# print("Radius = ", r);
#=====================================
# Driver code
#if __name__ == "__main__" :
#
# x1 = 1 ; y1 = 1;
# x2 = 2 ; y2 = 4;
# x3 = 5 ; y3 = 3;
# findCircle(x1, y1, x2, y2, x3, y3);
# This code is contributed by Ryuga
#Output:
#Centre = (3, 2)
#Radius = 2.23607
#ExampleGet your own Python Server
#Return the value of 4 to the power of 3 (same as 4 * 4 * 4):
#x = pow(4, 3)
#In Python, you use the double slash // operator to perform floor division. This // operator divides the first number by the second number and rounds the result down to the nearest integer (or whole number).
#----------------------------------------
AddLabel(1, " ", Color.BLACK);
def na = Double.NaN;
def bn = BarNumber();
#=====================================
script findCircle {
input x1 = 0;
input y1 = 0;
input x2 = 0;
input y2 = 0;
input x3 = 0;
input y3 = 0;
def x12 = x1 - x2;
def x13 = x1 - x3;
def y12 = y1 - y2;
def y13 = y1 - y3;
def y31 = y3 - y1;
def y21 = y2 - y1;
def x31 = x3 - x1;
def x21 = x2 - x1;
# x1^2 - x3^2
def sx13 = Power(x1, 2) - Power(x3, 2);
# y1^2 - y3^2
def sy13 = Power(y1, 2) - Power(y3, 2);
def sx21 = Power(x2, 2) - Power(x1, 2);
def sy21 = Power(y2, 2) - Power(y1, 2);
def f = Floor(((sx13) * (x12) + (sy13) * (x12) + (sx21) * (x13) + (sy21) * (x13)) / (2 * ((y31) * (x12) - (y21) * (x13))));
def g = Floor(((sx13) * (y12) + (sy13) * (y12) + (sx21) * (y13) + (sy21) * (y13)) / (2 * ((x31) * (y12) - (x21) * (y13))));
def c = (-Power(x1, 2) - Power(y1, 2) - 2 * g * x1 - 2 * f * y1);
# eqn of circle be x^2 + y^2 + 2*g*x + 2*f*y + c = 0
# where centre is (h = -g, k = -f) and
# radius r as r^2 = h^2 + k^2 - c
def h = -g;
def k = -f;
def sqr_of_r = h * h + k * k - c;
# r is the radius
def r = Round(Sqrt(sqr_of_r), 5);
plot cntr_x = h;
plot cntr_y = k;
plot radius = r;
}
#=====================================
# inputs
# x = bar# , y = $$
input x1 = 100;
input y1 = 50;
input x2 = 124;
input y2 = 66;
input x3 = 222;
# 3rd point is horz from point 1
def y3 = y1;
#-----------------------------
def centerx = findCircle(x1, y1, x2, y2, x3, y3).cntr_x;
def centery = findCircle(x1, y1, x2, y2, x3, y3).cntr_y;
def r = findCircle(x1, y1, x2, y2, x3, y3).radius;
AddLabel(1, "==========", Color.CYAN);
AddLabel(1, "point 1 " + x1 + " , " + y1, Color.YELLOW);
AddLabel(1, "==========", Color.CYAN);
AddLabel(1, "center " + centerx + " , " + centery, Color.YELLOW);
AddLabel(1, "radius " + r, Color.YELLOW);
#-----------------------------
#draw points
# point1
plot pt1 = if bn == x1 then y1 else na;
pt1.SetPaintingStrategy(PaintingStrategy.POINTS);
pt1.SetDefaultColor(Color.CYAN);
pt1.SetLineWeight(3);
pt1.HideBubble();
# point2
plot pt2 = if bn == x2 then y2 else na;
pt2.SetPaintingStrategy(PaintingStrategy.POINTS);
pt2.SetDefaultColor(Color.CYAN);
pt2.SetLineWeight(3);
pt2.HideBubble();
# point3
plot pt3 = if bn == x3 then y3 else na;
pt3.SetPaintingStrategy(PaintingStrategy.POINTS);
pt3.SetDefaultColor(Color.CYAN);
pt3.SetLineWeight(3);
pt3.HideBubble();
# center point
plot ctr = if bn == centerx then centery else na;
ctr.SetPaintingStrategy(PaintingStrategy.SQUARES);
ctr.SetDefaultColor(Color.YELLOW);
ctr.SetLineWeight(4);
ctr.HideBubble();
AddChartBubble(bn == centerx, centery,
"center"
, Color.YELLOW, no);
#-----------------------------
# draw lines between points
def slope12 = (y2 - y1) / (x2 - x1);
def slope23 = (y3 - y2) / (x3 - x2);
def rad1 = (centery - y1) / (centerx - x1);
# line , slope 1-2
def sl1 = if bn == 1 then na
else if bn == x1 then y1
else if (bn > x1 and bn <= x2) then sl1[1] + slope12
else na;
plot z1 = sl1;
# line , slope 2-3
def sl2 = if bn == 1 then na
else if bn == x2 then y2
else if (bn > x2 and bn <= x3) then sl2[1] + slope23
else na;
plot z2 = sl2;
# line , slope 1-3
def sl3 = if bn == 1 then na
else if bn == x1 then y1
else if (bn > x1 and bn <= x3) then sl3[1] + 0
else na;
plot z3 = sl3;
# line , radius , center to pt1
def r1 = if bn == 1 then na
else if bn == x1 then y1
else if (bn > x1 and bn <= centerx) then r1[1] + rad1
else na;
plot z4 = r1;
#-----------------------------
# circle_2points_0b
# draws a circle
#def circle_bars = if ( bn >= x11 and bn <= x22) then 1 else 0;
def arc_bars = if ( bn >= x1 and bn <= x3) then 1 else 0;
# -----------------------------------------
# def y = (pt1price + pt2price ) / 2;
#calc x and y center
#calc
#calc y
# y^2 = r^2 - x^2
# y = sqrt ( r^2 - x^2 )
# radius bars
#def rad1 = centerx - x11;
#def rad2 = floor((x11 + x22)/2);
#AddLabel(1, "rad0 " + rad0, Color.YELLOW);
#AddLabel(1, "rad1 " + rad1, Color.CYAN);
#addlabel(1, "rad2 " + rad2, color.cyan);
# if bars >= circle bars <= , centerx - bn
def barx = if !arc_bars then na else AbsValue(centerx - bn);
# y = sqrt ( r^2 - x^2 )
# circle_bars
def circley = if !arc_bars then na else Sqrt( (r * r) - (barx * barx) );
def yfactor = 1.0;
plot ytop = centery + (circley * yfactor);
#plot ybot = centery - (circley * yfactor);
#