Calculate the coordinates of a point in a line - geometry

I have a game represented like this :
I would like to calculate the coordinates of the point x4,y4.
What I know is :
y4 = y3, x4 is on the line x1,y1,x2,y2 and the line is 45° (degree)
I tried x4 = y4 - y1 + x1 but it doesn't work very well..
Any ideas ?

m= (y2-y1)/(x2-x1) = (y4-y1)/(x4-x1)=1 as slope is 45 degree.
so x4=x1+y4-y1;
substitute y4=y3;
then X4 = x1+y3-y1;

This video should help you to get the right formula.
Should be:
m = (y2 - y1) / (x2 - x1)
b = y2 - m * x2
x4 = (y3 - b) / m

Since the line is at a 45° angle, dx=dy between two points on the line. Thus:
x4 = x1+(y1-y4)

Related

Find coordinates for arrowhead [duplicate]

This question already has answers here:
Find coordinates to draw arrow head (isoscele triangle) at the end of a line
(2 answers)
Closed 17 days ago.
I have drawn a line (x1, y1), (x2, y2) and would now like to draw 2 more lines that would form an arrowhead at point (x2, y2). How do I determine the 2 points to which I need to draw the arrowheads from point (x2, y2)? I know that I need to find 2 points on a circle with center at point (x2, y2) that are equidistant from line (x1, y1), (x2, y2) but I do not know how to find this. Geometry is not my forte.
Your line is vector from (x1,y1) to (x2,y2), its components are
D = (dx, dy) = (x2-x1, y2-y1)
Length of vector (perhaps you have Hypot function in math library):
Norm = Sqrt(dx * dx + dy * dy)
Normalized (unit length) vector:
uD = (udx, udy) = (dx/Norm, dy/Norm)
(again - math library might contain a function for normalized vector to replace two last formulas)
Unit perpendicular vector:
uP = (upx, upy) = (-udy, udx)
Now make two points at distance L along line from line end, and with perpendicular distance W from the line:
ax = x2 - udx * L + W * upx
ay = y2 - udy * L + W * upy
bx = x2 - udx * L - W * upx
by = y2 - udy * L - W * upy
At last draw lines A-P2 and B-P2
P.S. If you want to define arrow angle, tg(alpha) = W/L, also you can look at calculations here

Coloring the area between two curves with bokeh

I've got a code with bokeh. There is two math functions where there is an area zone between these two functions in the interval [0, 2]. How can I fill this area zone with a color? I can't use polygon because it is not a polygon.
Here's the code:
import numpy as np
from bokeh.plotting import *
N = 300
x0 = np.linspace(-1, 4, N)
x1 = np.linspace(0, 4, N)
y0 = 0.5 * (x0 ** 2)
y1 = np.sqrt(2 * x1)
y2 = -y1
# output to static HTML file
output_file('plotting_areas.html')
TOOLS = 'pan, wheel_zoom, box_zoom, reset,save, box_select, lasso_select'
p = figure(tools=TOOLS, width=350, height=350,
title=None, x_range=(-1, 5), y_range=(-5, 5))
p.line(x0, y0)
p.line(x1, y1)
p.line(x1, y2)
show(p)
And here is an image for more details.
Thanks
There is nothing built in to Bokeh that will do, e.g. a flood fill, which is really what would be needed. Your best bet is to compute a polygonal approximation to the area yourself.
Otherwise you could (in principle) create a custom extension to perform a flood-fill in JavaScript, but I'm not sure how much effort that would take.
Ok, I've found the solution with bokeh and it is very simple and possible. The key is making two vectors (arrays) with the images of every two math functions between the OX interval. For each vector make a polygon with patch bokeh instruction without border line.
Here is the code:
import numpy as np
from bokeh.plotting import *
N = 300
x0 = np.linspace(-1, 4, N)
x1 = np.linspace(0, 4, N)
y0 = 0.5 * (x0 ** 2)
y1 = np.sqrt(2 * x1)
y2 = -y1
def f1(x):
return 0.5 * (x**2)
def f2(x):
return np.sqrt(2 * x)
z = np.zeros(N)
w = np.zeros(N)
x = np.linspace(0, 2, N)
for i in np.arange(len(x)):
z[i] = f1(x[i])
w[i] = f2(x[i])
# output to static HTML file
output_file('plotting_areas.html')
TOOLS = 'pan, wheel_zoom, box_zoom, reset,save, box_select, lasso_select'
p = figure(tools=TOOLS, width=350, height=350,
title=None, x_range=(-1, 5), y_range=(-5, 5))
p.line(x0, y0)
p.line(x1, y1)
p.line(x1, y2)
p.patch(x, z, color='red')
p.patch(x, w, color='red')
show(p)
And here is an image with the optimal solution:
Thanks
There is VArea now which should do the trick. Perhaps you might want to restict the plotting range to f1 > f2.

Calculating the centroids of two superposed gaussian functions

I am trying to find a solution to the following problem. I have a set of points which should model a sum of 2 Gaussian functions centered at different points. I need to find these two points. Up to now my approach has been to find the centroid of the whole set and cut the set of date below and above it. Then I calculate the centroid of each piece and those are my centers. This approach however cuts the information of, say, the left Gaussian which leaks into the right half of the data. This makes the procedure fail when the Gaussians are close together. Is there way to do this more intelligently? Due to the computational difficulty I would prefer if the solution didn't involve curve fitting.
As the OP does not show any data, it is not clear how noisy the data is. Furthermore, it is not clear how "close together" is defined here. In the following I have a simple approximation that works with low noise and the assumption that the left hand side data is dominated by the left Gaussian, while the right hand side is dominated by the right Gaussian. This gives some restrictions to position, height, and especially standard deviation.
It surely works for a single peak, but is quite OK for mixed double peaks ( within the above mentioned restrictions )
#!/usr/bin/python
import matplotlib.pyplot as plt
import numpy as np
def gaussian( x, x0, s, a):
return a * np.exp( -( x - x0 )**2 / ( 2 * s**2 ) ) / np.sqrt( 2 * np.pi * s**2 )
def get_x0( x1, x2, x3, y1, y2, y3 ):
l12= np.log( y1 / y2 )
l13= np.log( y1 / y3 )
return ( ( x2 + x1 )/2. - ( x3 + x1 )/2. * l12/l13 * ( x3 - x1 ) / ( x2 - x1 ) ) / ( 1 - l12 / l13 * (x3 - x1 ) / ( x2 - x1 ) )
fig = plt.figure( )
ax = fig.add_subplot( 2, 1, 1 )
xL = np.linspace(-8, 8, 150 )
yL = np.fromiter( ( gaussian( x,-2.1, 1.2, 8 ) for x in xL ), np.float )
marker=[10,15,20]
x1 = xL[ marker[0] ]
x2 = xL[ marker[1] ]
x3 = xL[ marker[2] ]
y1 = yL[ marker[0] ]
y2 = yL[ marker[1] ]
y3 = yL[ marker[2] ]
print get_x0( x1, x2, x3, y1, y2, y3 )
ax.plot( xL, yL )
ax.scatter( [ x1, x2, x3 ],[ y1, y2, y3 ])
bx = fig.add_subplot( 2, 1, 2 )
yL = np.fromiter( ( gaussian( x,-2.1, 1.2, 8) + gaussian( x,0.7, 1.4, 6 ) for x in xL ), np.float )
marker=[10,15,20]
x1 = xL[ marker[0] ]
x2 = xL[ marker[1] ]
x3 = xL[ marker[2] ]
y1 = yL[ marker[0] ]
y2 = yL[ marker[1] ]
y3 = yL[ marker[2] ]
bx.scatter( [ x1, x2, x3 ],[ y1, y2, y3 ])
print get_x0( x1, x2, x3, y1, y2, y3 )
marker=[-20,-25,-30]
x1 = xL[ marker[0] ]
x2 = xL[ marker[1] ]
x3 = xL[ marker[2] ]
y1 = yL[ marker[0] ]
y2 = yL[ marker[1] ]
y3 = yL[ marker[2] ]
bx.scatter( [ x1, x2, x3 ],[ y1, y2, y3 ])
print get_x0( x1, x2, x3, y1, y2, y3 )
bx.plot( xL, yL )
plt.show()
Shows:
#Single
-2.0999999999999455
#Double
-2.0951188129317813
0.6998760921436634
which is pretty close to -2.1 and 0.7
In case of noise some averaging might be required.

gnuplot: 3d surface, with 2d line graph on the y,z axis wall

I've made a 3d surface plot in gnuplot, and I'm wondering if it's possible to plot an unrelated 2d line graph on the 'wall' of the y,z axis.
So in essence, I take my x,y line plot but paste it on to the y,z axis.
Similar to a contour plot on the x,y, under a 3d surface, but different.
This may not be precisely what you are looking for, but it is a way to plot a function on a 'wall' of your splot box:
#!/usr/bin/env gnuplot
set term png
set output 'test.png'
# this is the function you want on the wall
myfun(x,y) = y**2
# set the ranges manually
xmin = -10
xmax = 10
ymin = -10
ymax = 10
x_range = xmax - xmin
y_range = ymax - ymin
scaler = 0.001
x1 = xmin - x_range*scaler
x2 = xmin + x_range*scaler
x3 = xmax - x_range*scaler
x4 = xmax + x_range*scaler
y1 = ymin - y_range*scaler
y2 = ymin + y_range*scaler
y3 = ymax - y_range*scaler
y4 = ymax + y_range*scaler
xminwall(x,y) = (x > x1 && x < x2) ? myfun(x,y) : 1/0
xmaxwall(x,y) = (x > x3 && x < x4) ? myfun(x,y) : 1/0
yminwall(x,y) = (y > x1 && y < x2) ? myfun(x,y) : 1/0
ymaxwall(x,y) = (y > x3 && y < x4) ? myfun(x,y) : 1/0
splot sin(x), xminwall(x,y)
Here is the result:
What the script does is plots the function you want to plot on the wall (myfun(x,y)) and does a splot of it, restricted to values that are very close to the wall. The surface mesh has a certain number of gridpoints, and one of those points is always at the x and y limits.
In this example, if I had just done splot sin(x), y**2, I would have gotten two overlapping surfaces.
I made four functions (xminwall, etc.) so you can plot on the wall at the x/y range minimum/maximum walls. Note that mufun(x,y) has to be a function of the correct x and y in order for the plot to show up properly on the wall, otherwise you might get a straight line.

Calculate the Z of a line intersection given the XY coordinates?

I'm intersecting a line in 2D and I calculate the X,Y coordinates of the intersection point. What I need is the Z of the intersection point given the X,Y,Z of the line points, and the X,Y of the intersection. From what I understand of equations it should be a one-liner but I don't know enough math to get there.
Your question is rather vague, but I will try to answer.
So take following equation:
Let's note it as Fx(X) = Fy(Y) = Fz(Z) and take a part of it:
Fx(X) = Fz(Z)
Then you said you know x, y and z for two points, put it to x1, x2, z1, z2 accordingly. Then put x of intersection to x. Now you have a linear equation with one variable z. Here it is:
z = (x - x1) / (x2 - x1) * (z2 - z1) + z1

Resources