I have a triangle as shown in the picture with A(109,239) ,B(182,234) and C(140,157).
I am using xlib programming to get this.
On a mouse click event at a point inside the triangle , i find the nearest vertex from that point and then i want to extent the line outside the triangle for a finitely large length.
Can any one give an idea how can i do this. What i think is we have to add some value ex a,b to B(x+a,y+b) , but i am not sure how will i calculate the value of a,b ?
If you know 2 points p0 and p1, you can calculate y for any x:
y = (x - x0) * (y1 - y0) / (x1 - x0) + y0
So in your case those 2 points would be mouse click point and the vertex point.
Edit
You could select x that is on opposite side of mouse click.
x = vertexX + (vertexX - mouseX);
Related
I would like to draw rounded area and points over a photo.
I decided to do it with a svg which is simplier to make it resizable and movable.
But, from a list of points I do not have an area which go through all points.
I used the Quadratic Bezier CurveTo, but I am not able to find the mathematic formula to give the the "Q parameter" the values to calculate the control point to go from A to C passing by B.
For the moment, when the angle is too high, the line "turn" before the point, but i would like to go touch the point.
There is infinite number of curves through point B.
Let define that B lies on the curve at parameter t=1/2
Quadratic curve with unknown control point Q has equation
P(t) = A*(1-t)^2 + 2*Q*t*(1-t) + C*t^2
Substiting point B and t=1/2, we have
B = A/4 + Q/2 + C/4
Q = 2*B - A/2 - C/2
or in coordinates
Q.x = 2*B.x - A.x/2 - C.x/2
Q.y = 2*B.y - A.y/2 - C.y/2
This very simple method should work well when B is near symmetrical relative to A and C
Q.x = 2*7 - 0 - 20/2 = 4
Q.y = 2*10 - 0 - 0 = 20
How can I find the coordinates of P ? I've seen other posts in 2D and also in 3D but they say that I need 3 angles for 3D and some say I only need two but I dont understand which ones, I suck at math.
All See this image, I have is those two angles and the distance between B and P
To define a vector in 3D, given its length, you need 2 angles. These 3 coordinates (length + 2 angles) are named "spherical coordinates". There are 3 conventions for defining such angles. The most common one is the radius-elevation-azimuth. In this convention, expression of the cartesian coordinates of the vector, given the radius, elevation angle and azimuth angle are:
x = radius * sin(elevation) * cos(azimuth)
y = radius * sin(elevation) * sin(azimuth)
z = radius * cos(elevation)
I try to do a thing that should be nothing more than a two-dimensional, linear interpolation but currently I fail finding the correct approach. To describe the problem a bit simplified: there is a drawing area with a size of 3000x3000 pixels where I have to draw e.g. a horizontal line. To do that I'm drawing dots or short lines from every pixel position to the next pixel position which then forms a line.
Now a correction has to be applied to the whole thing where correction information can be found in a (for this example simplified) 4 by 4 array, where every element contains a pair of coordinates describing the values after correction. So a neutral array (with no correction) would look like this:
0,0 1000,0 2000,0 3000,0
0,1000 1000,1000 2000,1000 3000,1000
0,2000 1000,2000 2000,2000 3000,2000
0,3000 1000,3000 2000,3000 3000,3000
A real correction table would contain other coordinates describing the correction to be done:
So as input data I have the coordinates of points on the line without correction, the fields values without correction and the correction data. But how can I calculate the lines points now applying the correction values to it so that a distorted line is drawn like shown in right side if the image? My current approach with two separate linear interpolations for X and Y does not work, there the Y-position jumps on a cells border but does not change smoothly within a cell.
So...any ideas how this could be done?
You have to agree on an interpolation method first. I would suggest either bilinear or barycentric interpolation. In one of my previous posts I visualized the difference between both methods.
I'll concentrate on the bilinear interpolation. We want to transform any point within a cell to its corrected point. Therefore, all points could be transformed separately.
We need the interpolation parameters u and v for the point (x, y). Because we have an axis-aligned grid, this is pretty simple:
u = (x - leftCellEdge) / (rightCellEdge - leftCellEdge)
v = (y - bottomCellEdge) / (topCellEdge - bottomCellEdge)
We could reconstruct the point by bilinear interpolation:
p2 p4
x----x
| o |
x----x
p1 p3
o = (1 - u) * ((1 - v) * p1 + v * p2) + u * ((1 - v) * p3 + v * p4)
Now, the same formula can be used for the corrected points. If you use the original points p1 through p4, you'll get the uncorrected line point. If you use the corrected cell points for p1 through p4, you'll get the corrected line point.
Im trying to do a graph from evalued math function and this is last think I need to do. I have graph with limit coordinates -250:-250 left down and 250:250 right up. I have Y-limit function, which is defined as -10:10, but it could be user redefined and if it is redefined, I need to calculate new coordinates.
I have now field of y-coordinates with 20000 values and each of is multiply by:
ratioY = 25 / (fabs( up-limit - down-limit ) / 20) which will make coordinates adapt for new Y-limit (if limit is -5:5, graph looks 2x bigger), this works good, but now isnt graph exactly where it should be (see pictures). Simply 25 is multiplied for postscript coordinates and (up-limit - down-limit) / 20 is ratio for "zooming" Y coordinates. This works fine.
Now Im trying to "move coordinates" which will subtracted from revaluated value:
ycoor = (ycoor * ratioY) - move-coorY ;.
Now I have something like this:
move-coorY = 25* ( ( up-limit - down-limit) /2 );
and it doesnt work correctly. I need to do sin(0) start from 0.
This is a correct graph which is -10:10
(source: matematika.cz)
This is a bad graph which is -5:10
(source: matematika.cz)
Maybe its easier not to do this with fixed numbers (like your ratioY) but with two different coordinate systems. Physical coordinates are in your problem domain, i.e. they are the real values of your sine curves. Logical coordinates refer to the device, in your case they are the point values in Postscript, but they might be pixels on a HTML canvas or whatever.
I'll denote the physical coordinates of the first axis with a small x and the corresponding logical coordinate with a capital X. In each coordinate system we have:
Lower bound: x_min, X_max
Upper bound: x_max, X_max
Range: dx = x_max - x_min
dX = X_max - X_min
Then you can calculate your logical coordinates from the physical ones:
X(x) = X_min + (x - x_min) * dX / dx
This also works vice versa, which is not an issue for Postscript files, but max be useful for an intractive canvas where a mouse click should yield the physical coordinates.
x(X) = x_min + (X - X_min) * dx / dX
In your case, the ratio or scale factor is dX / dx, which you can calculate once for each axis. Let's plot the first point with y == 0 in your first graph:
y_min = -10
y_max = 10
dy = 20
Y_min = -250
Y_max = 250
dX = 500
Y(0) = -250 + (0 - (-10)) * 500 / 20
= -250 + 10 * 500 / 20
= 0
In the second graph, the logical coordinates are the same, but:
y_min = -5
y_max = 10
dy = 15
Y(0) = -250 + (0 - (-5)) * 500 / 15
= -250 + 5 * 500 / 15
= -83.3333
If you change the range of your graph, e.g. from (-10, 10) to (-5, 10), just adjust the physical coordinates. If you resize your graph, change the logical coordinates. (Also, calculating the point in the graph is the same as calculating the position of the tick mark for the axis. Strangely, you got the tick marks right, but not your graph. I think your problem was to account for the non-zero lower bound in both graph and curve data.)
Also, it's probably better to re-evaluate the logical coordinates when printing instead of transfroming them from a previous plot. You can do that on the fly, so that you only need to keep the physical data in an array.
(And lastly, I'll admit that I'm not entirely sure these two kinds of cooirdinates are called physical and logical. I know these terms are used, but it may be the other way round or they might even mean sonething different altogether.)
My friend did a well yob for me and programmed this...
double zeroPosition(double startY, double endY){
double range = endY - startY;
double topSize = endY / range;
return 250.0 - 500 * topSize;
}
This will calculate position of zero, which I just add to my Y position with ratio and It works exactly how I need!
But thanks M Oehm ;)
I have two lines, one straight and one curvy. Both have an arbitrary number of x and y values defining the lines - the number of x and y values are not the same for either line. I am attempting to get separate distances of points between the curved line coordinates and the straight line coordinates. You can think of discrete integration to get a better picture of what I'm talking about, something along the lines of this: http://www.scientific-solutions.ch/tech/origin/products/images/calculus_integral.gif
By adding the different distances, I would get the area. The part on which I am stuck is the actual synchronization of the points. I can simply compare the x and y values of the straight and curve coordinates every ten indices for example because the curved coordinates are time dependent (as in the points do not change at a general rate). I need a way to synchronize the actual coordinates of the two sets of points. I thought about interpolating both sets of points to a specific number of points, but again, the time dependence of the curved set of points makes that solution void.
Could someone please suggest a good way of doing this, outlining the basics? I really appreciate the help.
Code to be tried (pseudo):
xLine = [value1 value2 ...]
yLine = [value1 value2 ...]
xCurve = [value1 value2 ...]
yCurve = [value1 value2 ...]
xLineInterpolate = %interpolate of every 10 points of x until a certain value. same with yLineInterpolate, xCurveInterpolate and yCurveInterpolate.
Then, I could just take the same index from each array and do some algebra to get the distance. My worry is that my line values increase at a constant rate whereas my curve values sometimes do not change (x and y values have different rates of change) and sometimes do. Would such an interpolation method be wrong then?
If I understand correctly, you want to know the distance between a straight line and a curve. The easiest way is to perform a coordinate transformation such that the straight line is the new x-axis. In that frame, the y-values of the curved line are the distances you seek.
This coordinate transformation is equal to a rotation and a translation, as in the following:
% estimate coefficients for straight line
sol = [x1 ones(size(x1))] \ y1;
m = sol(1); %# slope
b = sol(2); %# y-offset at x=0
% shift curved line down by b
% (makes the straight line go through the origin)
y2 = y2 - b;
% rotate the curved line by -atan(m)
% (makes the straight line run parallel to the x-axis)
a = -atan(m);
R = [+cos(a) -sin(a)
+sin(a) +cos(a)];
XY = R*[x2; y2];
% the distances are then the values of y3.
x3 = XY(1,:);
y3 = XY(2,:);
You need to use interpolation. I don't see how the time-dependence is relevant here - perhaps you are thinking of fitting a straight line to both curves? That's a bad idea.
You can do a simple interpolation for any curve just by assuming that every two adjacent points are connected by a straight line. This can be shown to be a reasonable approximation for the curve.
So, let's say you are looking at (x1,y1) and (x2,y2) which are adjacent to each other and now you choose an x3 that is between x1 and x2 (x1 < x2 < x3), and want to find the y3 value.
A simple way to find y3 is the following:
p=(x3-x1)/(x2-x1)
y3=y1+p*(y2-y1)
The idea is that p shows the relative position between x1 and x2 (0.5 would be the middle, for example), and then you use p as the relative position between y1 and y2.