Applying a translate-transformation (matrix(1 0 0 1 tx ty)) I get the new coordinates by just calculating x(new) = x + tx, y(new) = y + ty.
Applying a scale-transformation (matrix(sx 0 0 sy 0 0)) I just multiply:x(new) = x * sx, y(new) = y * sy.
Now here's my question: How can I do this for a rotation (with a rotation center other than 0,0)?
In general: How to compute the new coordinates one gets after applying a matrix (a b c d e f) in SVG?
I looked up some math.
It's a matrix-vector-multiplication. For SVG that means:
matrix(a b c d e f) corresponds to
x(new) = a*x + c*y + e
y(new) = b*x + d*y + f
Related
I have a set of data ([x[0],x[1]],y), many points in 3D space
and use scikit-learn to fit a learn model.
How I can calculate the distance between all the points to the fitting plane?
Does sklearn provide such function? I mean perpendicular distance.
My code works but too manually.
I am looking for an existing quick function in a package like sklearn.
Thanks.
def Linfit3D(x,y):
# x is a 2D array, they should be location of each bump, x_loc and y_loc
# y is the CTV or BTV that need to be fit to the least square plane
# three value will be returned, a,b, and c, which indicate a + b*x1 + c*x2 =y
model = sklearn.linear_model.LinearRegression()
model.fit(x, y)
coefs = model.coef_
intercept = model.intercept_
print("Equation: y = {:.5f} + {:.5f}*x1 + {:.5f}*x2".format(intercept, coefs[0],coefs[1]))
a=coefs[0]
b=coefs[1]
c=-1
d=intercept
return a,b,c,d
def point_to_plane_dist(x,y, a, b, c, d):
# the plane equation is: a*x + b*y + c*z + d = 0, and typically c=-1
# so the plane equation typicall is z = a*x + b*y + d
# and output has concerned the positive/negtive of point on top/bottom of the plane
f = abs((a * x[0] + b * x[1] + c * y + d))
e = (math.sqrt(a * a + b * b + c * c))
zp=a*x[0]+b*x[1]+d
# print('y = %2f, zp = %2f' %(y,zp))
if y>=zp:
return f/e
elif y<zp:
return (f/e)*(-1)
If I have a point B tangent to a circle with known radius r, and a point D outside the circle, how do I find the intersection of tangent lines through B and D?
If the only known values are the blue ones as shown in the sketch, how do I find point E?
I guess I'm missing the math background to combine similar examples with other known values to come to a solution.
We can write two vector equations:
-vector EB is perpendicular to radius CB, so dot product is zero
EB.dot.CB = 0 or
(ex - bx)*(bx - cx) + (ey - by)*(by - cy) = 0 (1)
-squared distance from center C to line DE is equal to squared radius (using vector product)
(DC x ED)^2 / |ED|^2 = R^2
((dx-cx)*(ey-dy)-(dy-cy)*(ex-dx))^2 = R^2 * ((ex-dx)^2+(ey-dy)^2) (2)
Equations (1) and (2) form equation system for two unknowns ex, ey. Solve it, get 0, 1 or 2 solutions (due to quadratic equation)
By running some more synthetic geometry first, you can apply the law of cosines on triangle BCD to express CD, then use in Pythagoras' theorem for triangle CDF to find the length d of of DF. Then, apply the law of cosines to the triangle BDE to find the length e of EF, where e = DE - d. Since EB = EF = e you just have to make the vector AB unit first and then multiply by e to find vector BE. After that just add the coordinates of B to BE.
The point H is the other point on the line AB such that the line DH is the other tangent to the circle.
import numpy as np
import math
'''
input A, B, D, r
'''
A = [ 0,-4]
B = [-1, 1]
D = [ 5, 0]
r = 2
A = [ 5.49, -8.12]
B = [ 1.24, 1.82]
D = [ 15.95, -1.12]
r = 3
A = np.array(A)
B = np.array(B)
D = np.array(D)
AB = B - A
l_AB = math.sqrt(AB[0]**2 + AB[1]**2)
AB = AB / l_AB
BD = D - B
l_BD = math.sqrt(BD[0]**2 + BD[1]**2)
cos_alpha = (-AB[0]*BD[0] - AB[1]*BD[1]) / l_BD
sin_alpha = math.sqrt(1 - cos_alpha**2)
d = math.sqrt( l_BD**2 - 2*r*l_BD*sin_alpha )
e = (l_BD**2 - d**2) / (2*d - 2*l_BD*cos_alpha)
E = B + e*AB
h = (l_BD**2 - d**2) / (2*d + 2*l_BD*cos_alpha)
H = B - h*AB
AB_perp = [AB[1], -AB[0]]
AB_perp = np.array(AB_perp)
C = B + r*AB_perp
CE = E - C
l2_CE = CE[0]**2 + CE[1]**2
G = C + (r**2 / l2_CE)*CE
F = B + 2*(G - B)
print('E =', E)
print('H =', H)
print('C =', C)
print('G =', G)
print('F =', F)
What is A and B so that the line Ay = Bx + 1 passes through points (1, 3) and (5,13) in the Cartesian plane?
I have been trying to solve it using the slope intercept equation to no avail. This is taken from Dale Hoffman's Contemprary Calculus.
First, I would reorder to get canonical form,
y = (B/A) * x + (1/A) = m * x + b
Now we find slope (m):
m = dy / dx = (13 - 3) / (5 - 1) = 2.5
sub in to find b:
3 = 2.5 * 1 + b
b = 0.5
Now sub back to find the values you want,
b = 0.5 = 1 / A
A = 2
m = 2.5 = B / 2
B = 5
I'm trying to write a program on CNC. Basically I have circular arc starting x, y , radius and finishing x, y also I know the direction of the arc clockwise or cc. So I need to find out the value of y on the arc at the specific x position. What is the best way to do that?
I found similar problem on this website here. But i not sure how to get angle a.
At first you have to find circle equation. Let's start point Pst = (xs,ys), end point Pend = (xend,yend)
For simplicity shift all coordinates by (-xs, -ys), so start point becomes coordinate origin.
New Pend' = (xend-xs,yend-ys) = (xe, ye), new 'random point' coordinate is xr' = xrandom - xs, unknown circle center is (xc, yc)
xc^2 + yc^2 = R^2 {1}
(xc - xe)^2 + (yc-ye)^2 = R^2 {2} //open the brackets
xc^2 - 2*xc*xe + xe^2 + yc^2 - 2*yc*ye + ye^2 = R^2 {2'}
subtract {2'} from {1}
2*xc*xe - xe^2 + 2*yc*ye - ye^2 = 0 {3}
yc = (xe^2 + ye^2 - 2*xc*xe) / (2*ye) {4}
substitute {4} in {1}
xc^2 + (xe^2 + ye^2 - 2*xc*xe)^2 / (4*ye^2) = R^2 {5}
solve quadratic equation {5} for xc, choose right root (corresponding to arc direction), find yc
having center coordinates (xc, yc), write
yr' = yc +- Sqrt(R^2 -(xc-xr')^2) //choose right sign if root exists
and finally exclude coordinate shift
yrandom = yr' + ys
equation of a circle is x^2 + y^2 = r^2
in your case, we know x_random and R
substituting in knows we get,
x_random ^ 2 + y_random ^ 2 = R ^ 2
and solving for y_random get get
y_random = sqrt( R ^ 2 - x_random ^ 2 )
Now we have y_random
Edit: this will only work if your arc is a circular arc and not an elliptical arc
to adapt this answer to an ellipse, you'll need to use this equation, instead of the equation of a circle
( x ^ 2 / a ^ 2 ) + ( y ^ 2 / b ^ 2 ) = 1, where a is the radius along the x axis and b is the radius along y axis
Simple script to read data from a file called data.txt and compute a series of y_random values and write them to a file called out.txt
import math
def fromFile():
fileIn = open('data.txt', 'r')
output = ''
for line in fileIn:
data = line.split()
# line of data should be in the following format
# x h k r
x = float(data[0])
h = float(data[1])
k = float(data[2])
r = float(data[3])
y = math.sqrt(r**2 - (x-h)**2)+k
if ('\n' in line):
output += line[:-1] + ' | y = ' + str(y) + '\n'
else:
output += line + ' | y = ' + str(y)
print(output)
fileOut = open('out.txt', 'w')
fileOut.write(output)
fileIn.close()
fileOut.close()
if __name__ == '__main__':
fromFile()
data.txt should be formatted as such
x0 h0 k0 r0
x1 h1 k1 r1
x2 h2 k2 r2
... for as many lines as required
If I have six variables representing two lines in general equation form (ax + by + c = 0). So for example:
ax + by + c = 0
jx + ky + l = 0
How can I find the intersection point's (x and y) [assuming there is one] from the six variables?
PS. Any recommendation of a good source for information on very simply computational geometry, like this, would be appreciated.
The intersection point satisfies both equations. So all you need is to solve them simultaneously:
ax + by + c = 0 (*j)
jx + ky + l = 0 (*a)
ajx + bjy + cj = 0 (-)
ajx + aky + al = 0
(bj-ak)y + cj - al = 0
y = (al-cj) / (bj-ak)
And similarly for x. (Or you can substitute the found value for y in any of the original equations and then find x):
x = (ck-bl) / (bj-ak)