Finding the angle in a right-angle triangle with python 3 - python-3.x

I manage to solve this question for only 1 case out of the 5 in the system. I'm certain that my method is correct, but for some reason it doesn't work out for other cases.
Below is my code
import math
AB = int(input("Enter a value for side AB: "))
while(AB>100 and AB<=0):
AB = input("Enter a value for side AB: ")
BC = int(input("Enter a value for side BC: "))
while(BC>100 and BC<=0):
BC = input("Enter a value for side BC: ")
hyp = math.sqrt(math.pow(AB,2) + math.pow(BC,2)) #find side AC
mhyp = hyp/2 #find side MC
sind = (mhyp/BC) #sin(deg)=opp/hypotenuse
degree = round(((math.asin(sind))/((math.pi)*2))*360,0) #find the angle
print("%d" %(degree) + "°")
For the case when AC and BC are 10, it did yield 45 degrees as the angle. But when AC=1 and BC=100, it produces an error since arcsine cannot accept value beyond 1.7 radians. Same goes for AC=20 and BC=10 and AC=100 and BC=1..
Is this question solvable?

It is a matter of primarily your angle coming in radians and then you converting it into degrees. Rest everything then will fall in place :)
Also, We can see that BM is equal to MC basis the median to hypotenuse property making the triangle MBC as isosceles and hence Angle MBC = Angle MCB.
import math
if __name__ == '__main__':
AB = input()
assert 0<int(AB)<=100
BC = input()
assert 0<int(BC)<=100
assert (int(AB) >= 0 and float(AB).is_integer() == True) and (int(BC) >= 0 and float(BC).is_integer() == True)
AC = math.sqrt((int(AB)**2) + (int(BC)**2))
tan_acb_rad = int(AB)/int(BC)
acb_rad = math.atan(tan_acb_rad) #the angle comes in radians
acb_deg = math.degrees(acb_rad) #you have to convert it into degrees
ang_mbc = acb_deg
print(str(int(round(ang_mbc,0)))+u"\N{DEGREE SIGN}") #adding the degree symbol at end

import math
AB =int(input())
BC =int(input())
degree=u'\N{DEGREE SIGN}'
print(str(int(round(math.degrees(math.atan(AB/BC)))))+degree)

I know this isn't exactly your case but it still might explain your issue
Inverse Sine of a Value Greater than One
Think about what the sine wave or curve looks like. As the angles
change, the sine of the angle goes up and down, but it never goes
above 1 or below -1. In other words, there is no angle with a sine
that is greater than 1.
When you use the sine key, you put in an angle and get out the sine of
that angle. For instance, when you do sin(30) you are finding the
sine of 30 degrees and the calculator says it's 0.5. When you use the
inverse sine (shift-sine) you put in the value of the sine and the
calculator tells you the angle. So, the inverse sine of 0.5 is 30
since a 30 degree angle has a sine of 0.5.
So when you ask the calculator to do the inverse sine of 1.732, you
are asking it what angle has a sine of 1.732. But as we said above,
there is no angle that has a sine greater than 1. The inverse sine or
arcsin of 1.732 does not exist. That's what the calculator is saying.
It sounds like your problem is asking you to try to find angle B in a
triangle with a = 40, b = 80, and A = 60 degrees. Try constructing
such a triangle, and see what happens. We make angle A and mark 80
units on one of its rays for side b, then swing an arc around the
resulting point C with a radius or length of 40, so that its
intersection with the other ray will give point B. What happens?
B
/
/
/
/
/
c / ooooooooo
/ oooo oooo
/ ooo \ ooo
/ oo \
/ o \ a=40
/ o \
/ o \
/ o \
/60 o \
A----------------o----------------C-----------
b=80
So the calculator is correct: there is no such triangle! Obviously we
can't measure angle B at the top if side 'a' is not long enough to
complete the triangle and form that angle.

You made mistake in geometry - BM is median, not a height to AC (they accidentally coincide for isoseles right-angle triangle, but differ in general case).
So angle BMC is not right in general case, and you cannot get sin(theta) as ratio of MC and BC
But there is well-known property of right triangle - center of circumcircle lies in the middle of hypotenuse, so M point in this case is circumcenter.
This fact immediately tells us that BM=MC (two radii), BMC triangle is isosceles and theta = ACB angle.
Solution is pretty simple (note atan using):
import math
AB = 1
BC = 1
theta = math.atan(AB / BC)
degrees = round(theta * 180 / math.pi)
print(degrees)
>> 45
AB = 3
BC = 4
...
>> 37

Related

print a float value as degrees in python 3

Given a 90 degree angle, print the bisecting angle in degrees.
import math
#given angle
abc = 90
#m is midpoint of ac, therefor
abm = abc/2
mbc = abc - abm
degrees = math.degrees(mbc)
print(degrees)
but when I print it out, i get 2578.3100780887044
if I don't use math.degrees() then I get:
mbc = abc - abm
#degrees = math.degrees(mbc)
print(mbc)
45.0
but what I need is: 45°
I found math.radians() and math.degrees() but I feel like there is something I am missing. Any help?
Python 3 returns a float for every integer or float division. If you want you can floor the result writing: abm = int(abc/2). This will give you mbc as an integer.
On the other hand, math.degrees() converts an angle from radians to degrees, giving a float as a result. If you want to floor the result of the conversion you can do degrees = int(math.degrees(mbc)).

Is it accurate to conclude the radius of a circle given 4 bazier curves in svg?

I have used svg2paths2, and wanted to figure out what is the position and radius of a circle, I have noticed the circle is consructed by 4 CubicBezier, as follow:
Path(CubicBezier(start=(127.773+90.5469j), control1=(127.773+85.7656j), control2=(123.898+81.8906j), end=(119.121+81.8906j)),
CubicBezier(start=(119.121+81.8906j), control1=(114.34+81.8906j), control2=(110.465+85.7656j), end=(110.465+90.5469j)),
CubicBezier(start=(110.465+90.5469j), control1=(110.465+95.3281j), control2=(114.34+99.1992j), end=(119.121+99.1992j)),
CubicBezier(start=(119.121+99.1992j), control1=(123.898+99.1992j), control2=(127.773+95.3281j), end=(127.773+90.5469j)))
I have read the standard approach is to divide the circle into four equal sections, and fit each section to a cubic Bézier curve.
So I was wondering is it accurate to say the Radius of the circle is
(q1.start.real - q3.start.real)/2
or
(q2.start.imag - q4.start.imag)/2
And the center of the circle is:
c_x = (q1.start.real + q1.end.real) / 2
c_y = (q1.start.imag + q1.end.imag) / 2
Thank you!
I'm assuming you are using svg.path library in python, or svg2paths2 is related.
from svg.path import Path, Line, Arc, CubicBezier, QuadraticBezier, Close
path = Path(CubicBezier(start=(127.773+90.5469j), control1=(127.773+85.7656j), control2=(123.898+81.8906j), end=(119.121+81.8906j)),
CubicBezier(start=(119.121+81.8906j), control1=(114.34+81.8906j), control2=(110.465+85.7656j), end=(110.465+90.5469j)),
CubicBezier(start=(110.465+90.5469j), control1=(110.465+95.3281j), control2=(114.34+99.1992j), end=(119.121+99.1992j)),
CubicBezier(start=(119.121+99.1992j), control1=(123.898+99.1992j), control2=(127.773+95.3281j), end=(127.773+90.5469j)))
q1 = path[0]
q2 = path[1]
q3 = path[2]
q4 = path[3]
.real is the X coordinate
.imag is the Y coordinate
There's a very slight error in accuracy in the drawing program you are using and it's not at all an issue unless you want extreme accuracy.
(q1.start.real - q3.start.real) / 2 # 8.6539 is the radius in this case.
(q4.start.imag - q2.start.imag)/2 # 8.6543 is also the radius.
(q1.start.real - q1.end.real) # 8.6539 is again also the radius.
This accesses the same property, q1 of path and I' prefer it to the two above ways because it's accessing one property not two.
Below shown by green circle in diagram
c_x = (q1.start.real + q1.end.real) / 2 # 123.447 not center x
c_y = (q1.start.imag + q1.end.imag) / 2 # 86.21875 not center y
Below shown by red circle in diagram
c_x = q1.end.imag # 119.121 this is center x
c_y = q1.start.real # 90.5469 this is center y
To explain how serious the error in accuracy, the pink circle uses 8.6543 radius, below it is 8.6539 in green, perhaps viewable with an extreme zoom. But this does illustrate how important or not the decimal points can be.
Consider using numbers under 100 and as few decimal points as possible, especially understanding a new idea. Shorter text-length numbers vastly improves readability, understanding no end.
I often use just numbers below ten.
Note: you are drawing the circle counter-clockwise. Clockwise is the usual way.

How to get a point on an ellipse's outline given an angle?

So, I have ellipses given - they are defined by their midpoint, an horizontal radius(rh) and an vertical radius(rv). I'm drawing them using sin/cos and the result looks fairly good to me(just making sure this isn't an error source).
Now say I have an angle(or a direction vector) given and I want to have the point on the ellipse's outline with that angle/direction. My intuitive approach was to simply use the direction vector, normalise it and multiply its x-component with rh, its y-component with rv. Now both my written program AND all the calculations I did on a paper give me not the point I want but another one, though it's still on the ellipse's outline. However, this method works just fine if the direction is one of (1,0), (0, 1), (-1, 0), (0, -1), (so it works for 0°, 90°, 180°, 270°).
Although there is a farily big amount of data about ellipses themselves on the internet, I couldn't find any information about my particular problem - and I couldn't come up with any better solution than the above one.
So, any idea how to achieve this?
If I understand what you are asking then I think that what you need is polar form of an ellipse where the angle is measured from the ellipse centre. Using this form of the ellipse, you will be able to evaulate your elliptic radius value for a given choice of theta and then plot your point.
If you take a look at this gif image you will see why using the parametric angle give you the correct result only at theta = 90, 180, 270 and 360 degrees http://en.wikipedia.org/wiki/File:Parametric_ellipse.gif . Use the polar form for an ellipse and you should get the points that you want.
You are correct - the parametric angle is not the same as the angle between the desired point and the X axis. However, their tangents are proportional (with a factor of rh/rv) so you can use this approach:
Get the tangent of the desired angle
Multiply this tangent by rh/rv
Use trigonometric identities to compute the sine and cosine from the tangent
Scale/position the point according to the parameters (midpoint, rh, rv)
In Python:
from math import copysign, cos, sin, sqrt
class Ellipse:
def __init__(self, mx, my, rh, rv):
self.mx = mx
self.my = my
self.rh = rh
self.rv = rv
def pointFromAngle(self, a):
c = cos(a)
s = sin(a)
ta = s / c ## tan(a)
tt = ta * self.rh / self.rv ## tan(t)
d = 1. / sqrt(1. + tt * tt)
x = self.mx + copysign(self.rh * d, c)
y = self.my + copysign(self.rv * tt * d, s)
return x, y

Best fit square to quadrilateral

I've got a shape consisting of four points, A, B, C and D, of which the only their position is known. The goal is to transform these points to have specific angles and offsets relative to each other.
For example: A(-1,-1) B(2,-1) C(1,1) D(-2,1), which should be transformed to a perfect square (all angles 90) with offsets between AB, BC, CD and AD all being 2. The result should be a square slightly rotated counter-clockwise.
What would be the most efficient way to do this?
I'm using this for a simple block simulation program.
As Mark alluded, we can use constrained optimization to find the side 2 square that minimizes the square of the distance to the corners of the original.
We need to minimize f = (a-A)^2 + (b-B)^2 + (c-C)^2 + (d-D)^2 (where the square is actually a dot product of the vector argument with itself) subject to some constraints.
Following the method of Lagrange multipliers, I chose the following distance constraints:
g1 = (a-b)^2 - 4
g2 = (c-b)^2 - 4
g3 = (d-c)^2 - 4
and the following angle constraints:
g4 = (b-a).(c-b)
g5 = (c-b).(d-c)
A quick napkin sketch should convince you that these constraints are sufficient.
We then want to minimize f subject to the g's all being zero.
The Lagrange function is:
L = f + Sum(i = 1 to 5, li gi)
where the lis are the Lagrange multipliers.
The gradient is non-linear, so we have to take a hessian and use multivariate Newton's method to iterate to a solution.
Here's the solution I got (red) for the data given (black):
This took 5 iterations, after which the L2 norm of the step was 6.5106e-9.
While Codie CodeMonkey's solution is a perfectly valid one (and a great use case for the Lagrangian Multipliers at that), I believe that it's worth mentioning that if the side length is not given this particular problem actually has a closed form solution.
We would like to minimise the distance between the corners of our fitted square and the ones of the given quadrilateral. This is equivalent to minimising the cost function:
f(x1,...,y4) = (x1-ax)^2+(y1-ay)^2 + (x2-bx)^2+(y2-by)^2 +
(x3-cx)^2+(y3-cy)^2 + (x4-dx)^2+(y4-dy)^2
Where Pi = (xi,yi) are the corners of the fitted square and A = (ax,ay) through D = (dx,dy) represent the given corners of the quadrilateral in clockwise order. Since we are fitting a square we have certain contraints regarding the positions of the four corners. Actually, if two opposite corners are given, they are enough to describe a unique square (save for the mirror image on the diagonal).
Parametrization of the points
This means that two opposite corners are enough to represent our target square. We can parametrise the two remaining corners using the components of the first two. In the above example we express P2 and P4 in terms of P1 = (x1,y1) and P3 = (x3,y3). If you need a visualisation of the geometrical intuition behind the parametrisation of a square you can play with the interactive version.
P2 = (x2,y2) = ( (x1+x3-y3+y1)/2 , (y1+y3-x1+x3)/2 )
P4 = (x4,y4) = ( (x1+x3+y3-y1)/2 , (y1+y3+x1-x3)/2 )
Substituting for x2,x4,y2,y4 means that f(x1,...,y4) can be rewritten to:
f(x1,x3,y1,y3) = (x1-ax)^2+(y1-ay)^2 + ((x1+x3-y3+y1)/2-bx)^2+((y1+y3-x1+x3)/2-by)^2 +
(x3-cx)^2+(y3-cy)^2 + ((x1+x3+y3-y1)/2-dx)^2+((y1+y3+x1-x3)/2-dy)^2
a function which only depends on x1,x3,y1,y3. To find the minimum of the resulting function we then set the partial derivatives of f(x1,x3,y1,y3) equal to zero. They are the following:
df/dx1 = 4x1-dy-dx+by-bx-2ax = 0 --> x1 = ( dy+dx-by+bx+2ax)/4
df/dx3 = 4x3+dy-dx-by-bx-2cx = 0 --> x3 = (-dy+dx+by+bx+2cx)/4
df/dy1 = 4y1-dy+dx-by-bx-2ay = 0 --> y1 = ( dy-dx+by+bx+2ay)/4
df/dy3 = 4y3-dy-dx-2cy-by+bx = 0 --> y3 = ( dy+dx+by-bx+2cy)/4
You may see where this is going, as simple rearrangment of the terms leads to the final solution.
Final solution

Arctan > 1 newbie question

It has been quite some time since I've had to compute the theta of an angle. But given a right angle:
|
|
b |
-----------------
a
I'm trying to compute theta (the slope of the angle). My understanding of trigonometry (as rusty as it is) is that theta = arctan(b/a). So if b = 50 and a = 1811. Then using the windows calculator, 50 / 1811 = 0.027609055770292655991165102153506. Therefore the arctan(b/a) = 1.5814806205083755492980816356377. If my math is correct, how do I translate this value into the slope of the angle? It should be around 30-40 degrees, right?
atan2(y, x)
will return you the angle in radians (and successfully cope with the cases where x and/or y are 0).
To convert to degrees apply the following formula:
double degrees = radians * (180 / PI)
Where PI is 3.141592... or math.pi in c#
If you use a C dialect then there a useful function for just this purpose
atan2(y, x);

Resources