Basically I wish to draw a curve between 3 points in openGL as the below image indicates. I've found a couple of segments of code which would be useful for drawing a bezier curve using 4 points but for 3 points I've had really no success.
From the definition of a Bezier curve you have the following formula (for each x, y component):
x(t) = (1-t)^3*p1x + 3*t*(1-t)^2*c1x + 3*t^2*(1-t)*c3x + t^3*p3x
y(t) = (1-t)^3*p1y + 3*t*(1-t)^2*c1y + 3*t^2*(1-t)*c3y + t^3*p3y
In your case you know the mid point (p2x,p2y). You can also assume that c1x and c2x have the same value; and that c1y and c2y also have the same value
So we have the following equations at t=0.5
p2x = (3/4)*c1x+(p1x+p3x)/8
p2y = (3/4)*c1y+(p1y+p3y)/8
which are solved for c1x=c2x and c1y=c2y with
c1x = c2x = -(p1x-8*p2x+p3x)/6
c1y = c2y = -(p1y-8*p2y+p3y)/6
to give the final Bezier equation to use in terms of points (p1x,p1y), (p2x,p2y) and (p3x,p3y):
x(t) = (1-t)^3 * [p1x]
+ 3*t*(1-t)^2 * [-(p1x-8*p2x+p3x)/6]
+ 3*t^2*(1-t) * [-(p1x-8*p2x+p3x)/6]
+ t^3 * [p3x]
y(t) = (1-t)^3 * [p1y]
+ 3*t*(1-t)^2 * [-(p1y-8*p2y+p3y)/6]
+ 3*t^2*(1-t) * [-(p1y-8*p2y+p3y)/6]
+ t^3 * [p3y]
Summary
Try the four control points
( p1x, p1y )
( -(p1x-8*p2x+p3x)/6, -(p1y-8*p2y+p3y)/6 )
( -(p1x-8*p2x+p3x)/6, -(p1y-8*p2y+p3y)/6 )
( p3x, p3y )
Here is an example I made with p1=(0,0), p2=(2,2) and p3=(4,-1). I calculated the following control points
( 0, 0 )
( 2, 17/6 )
( 2, 17/6 )
( 4, -1)
with the results shown below:
Sounds like you want a Hermite spline.
Related
Could someone help me in understanding what paramaters assume to have such spiral as in this question:Draw equidistant points on a spiral?
I don't understant this parameter: rotation- Overall rotation of the spiral. ('0'=no rotation, '1'=360 degrees, '180/360'=180 degrees) I would be grateful if someone write some sets of parameters (sides,coils,rotation) to get spiral.
It's code in Matlab:
clc
clear all
centerX = 0
centerY = 0
radius = 10
coils = 30
rotation = 360
chord = 2
delta = 1
thetaMax = coils * 2 * pi;
awayStep = radius / thetaMax;
i = 1
for theta = (chord / awayStep):thetaMax;
away = awayStep * theta;
around = theta + rotation;
x(i) = centerX + cos ( around ) * away;
y(i) = centerY + sin ( around ) * away;
i = i + 1
theta = theta + (chord / away);
theta2 = theta + delta
away2 = away + awayStep * delta
delta = 2 * chord / ( away + away2 )
delta = 2 * chord / ( 2*away + awayStep * delta )
2*(away + awayStep * delta ) * delta == 2 * chord
awayStep * delta * 2 + 2*away * delta - 2 * chord == 0
a= awayStep; b = 2*away; c = -2*chord
delta = ( -2 * away + sqrt ( 4 * away * away + 8 * awayStep * chord ) ) / ( 2 * awayStep );
theta = theta + delta;
end
v = [0 x]
w = [0 y]
scatter(v,w)
Thank you in advance
I have a 2d matrix created from position, scale and rotation (no skew). I would like to be able to decompose this matrix back to the original components and have managed to do so with the following pseudo code:
posX = matrix.tx
posY = matrix.ty
scaleX = Sqrt( matrix.a * matrix.a + matrix.b * matrix.b )
scaleY = Sqrt( matrix.c * matrix.c + matrix.d * matrix.d )
rotation = ATan2( -matrix.c / scaleY, matrix.a / scaleX )
However this obviously only works with positive scale values and I am unsure how to calculate the correct negative scales. I have attempted various suggestions found using google but so far none have worked correctly.
I have tried the accepted answer from here and the decomposition explained here, whilst they produce correct transformations, the components of scale and rotation do not match my original values.
I have tried taking the sign of the diagonal matrix.a * matrix.d which appears to work for the scale on the x axis but unsure if this is the correct approach and can't figure out how to handle the y axis.
Is this even possible? Will I have to accept that I will not get back the exact components and the best I can hope for is values that produce the same transformation?
Any help or pointers would be greatly appreciated.
Original
Translation = 204, 159
Rotation = -3.0168146900000044
Scale = -3, -2
Matrix = [ 2.976675975304773, 0.37336327891663146, -0.24890885261108764, 1.984450650203182, 204, 159 ]
Decomposition
Translation = 204, 159
Rotation = 0.1247779635897889
Scale = 3, 2
Matrix = [ 2.976675975304773, 0.3733632789166315, -0.24890885261108767, 1.984450650203182, 204, 159 ]
That was using the following decomposition code:
posX = matrix.tx
posY = matrix.ty
scaleX = Sgn( a ) * Sqrt( matrix.a * matrix.a + matrix.b * matrix.b )
scaleY = Sgn( d ) * Sqrt( matrix.c * matrix.c + matrix.d * matrix.d )
rotation = ATan2( -matrix.c / scaleY, matrix.a / scaleX )
Sometimes you can't tell flip (negative scale) from rotation, e.g. an image flipped horizontally and vertically is identical to the image rotated by 180 degrees.
So what you have to do is know whether the transformation matrix contains flips. With that knowledge, you can cancel out the flip first, decompose it as usual, and put the flip back into the decomposed scale factors.
Pseudo code:
Matrix m
hFlip = true
vFlip = true
if hFlip: m = compose(m, scale(-1, 1))
if vFlip: m = compose(m, scale(1, -1))
translation, rotation, scale = decompose(m)
if hFlip: scale.x = -scale.x
if vFlip: scale.y = -scale.y
With regard to the attached image, I need to have a calculation Algorithm to move Axis A down n inches, and Axis B Left to right m inches in order for component circle D to follow the curve of the parabola; circle D is not always 10 inches and can be smaller. I am not a math major so this is a bit more complicated for me. I know I have an Arc length on Axis A which must be calculated (I don't know how to do this.), I then have an Arc Length on Axis B as well and that the arc is moving in reference to the position of Axis A , this arc length associated with Diameter of Circle D will determine where on the parabola the point of intersection between Circle D and the Parabola is . In order to follow the curve of the parabola starting from left to right or vice versa - I need a formula to follow the parabola. Accounting for D changing size. Can somebody provide some answers as to how to do this ; a nice formula with some explanatory information - at least enough detail that I can do a search on those parts and bits to get an understanding of what to do.
I have looked and found some information that may be helpful to me but does not answer my question at all:
https://stackoverflow.com/questions/4039039/fastest-way-to-fit-a-parabola-to-set-of-points
It seems that you need to calculate the curve resulting as an offset of a parabola.
In the C++ program that follows I'll show how to first find the formula of the parabola, then how to calculate the offset from that curve and finally how to find the angles which the two axis form to each other.
I consider point (0,0) as the left extreme of the parabola, the bottom of that (vertex) will be at the coordinates (12,-8.75), while the right extreme at (24,0). Take this picture as a reference (the parabola is blue, the trajectory of the center of the circle is orange):
Be aware that if the circle is too big, while it's tangent on one side it can intersect the parabola on the other side. I'm not sure if 12" is the total width of the parabola or only one half, but in the latter case a 10" tool will be too large:
The program will print out some samples (25) of the coordinates of the points where the circle representing the tool is tangent to the parabola, the corresponding coordinates of the center of the circle (the position of the tool) and the angles of the two axis (alpha and beta).
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
using std::cout;
using std::setw;
using std::vector;
int main() {
// set number of steps or points of approximation
int n_steps = 25;
// declare vectors to store coordinates into
vector<double> x2(n_steps), y2(n_steps);
// calculate the parameters of the parabola expressed by the formula
// y = ax^2 + bx + c
// Knowing 2 points, one of which is the vertex.
// xv = -b/2a | b = -2axv
// y0 = ax0^2 + bx0 + c => | yv - y0 = a(xv^2 - x0^2) + b(xv - x0)
// yv = axv^2 + bxv + c | yv - y0 = a(xv - x0)(xv + x0) + b(xv - x0)
//
// a ((xv - x0)*(xv + x0) - 2xv(xv - x0)) = yv - y0
// a (xv - x0)*(xv + x0 - 2xv) = yv - y0
// Known coordinates
double xv = 12.0,
yv = -8.75,
x0 = 0.0,
y0 = 0.0;
double dx = xv - x0,
a = (y0 - yv) / ( dx * dx ),
b = - 2.0 * a * xv,
c = y0 - x0 * ( a * x0 + b );
cout << "Parabola formula:\n"
<< "y = " << a << "x^2 + " << b << "x + " << c << "\n\n"
<< "max acceptable diameter: " << 1.0 / a << "\n\n";
// Coordinates of rotating axes, extrapolated from your drawing
double r1 = 13,
r2 = 9,
x1 = xv - r1,
y1 = r2;
// some helper values (constant) I'll use later
double rad_to_deg = 180.0 / M_PI,
r1quad = r1 * r1,
r2quad = r2 * r2,
rdif = r1quad - r2quad,
rsum = r1quad + r2quad,
rden = 1.0 / ( 2.0 * r1 * r2 );
// radius of the circle (tool)
double diameter = 10,
radius = diameter / 2.0;
cout << "Diameter of tool (circle): " << diameter << "\n\n";
// calculate parabola points
cout << "\t\t\tTangent\t\t\t\tCenter of circle\t\t alpha\t\tbeta\n";
// xt[0] = x0 xt[n_steps] = x0 + 2*(xv - x0)
double step = 2.0 * dx / ( n_steps - 1 );
for ( int i = 0; i < n_steps; ++i ) {
// calculate the tangent points which lies on the parabola
double xt = x0 + i * step,
yt = xt * ( a * xt + b ) + c;
// calculate the offset points, coordinates of the center of the circle
// first derivative of the parabola
double delta = 2.0 * a * xt + b;
// point perpendicular to the tangent at distance equal to radius
double k = radius / sqrt(delta * delta + 1.0);
x2[i] = xt - k * delta;
y2[i] = yt + k;
// distance from x,y to x1,y1
double dx1 = x2[i] - x1,
dy1 = y2[i] - y1,
r3quad = dx1 * dx1 + dy1 * dy1,
r3 = sqrt(r3quad);
// Now that I know the coordinates of the vertices of the triangle
// and the lengths of its sides I can calculate the inner angles
// using Carnot teorem, for example: a^2 = b^2 + c^2 - 2bc*cos(alpha)
double alpha_Carnot = acos((rdif + r3quad) / (2.0 * r1 * r3)),
beta_Carnot = acos((rsum - r3quad) * rden);
// angle to the orizzontal of line from x1,y1 to x,y in radians
double gamma = atan2(dy1,dx1);
// angle of Axis A to the orizzontal in degrees
double alpha = (gamma + alpha_Carnot) * rad_to_deg;
// angle of Axis B to Axis A. beta = 0 if parallel
double beta = beta_Carnot * rad_to_deg - 180.0;
// output the coordinates
cout << std::fixed << setw(4) << i << setw(10) << xt << setw(10) << yt
<< setw(15) << x2[i] << setw(10) << y2[i]
<< setw(15) << alpha << setw(12) << beta << '\n';
}
return 0;
}
This is the output:
Parabola formula:
y = 0.0607639x^2 + -1.45833x + 0
max acceptable diameter: 16.4571
Diameter of tool (circle): 10
Tangent Center of circle alpha beta
0 0.000000 0.000000 4.123644 2.827642 -7.228866 -142.502245
1 1.000000 -1.397569 5.003741 1.597437 -7.151211 -132.856051
2 2.000000 -2.673611 5.860925 0.503378 -7.962144 -123.965745
3 3.000000 -3.828125 6.690144 -0.454279 -9.159057 -115.700562
4 4.000000 -4.861111 7.485392 -1.276137 -10.496232 -108.022957
5 5.000000 -5.772569 8.239777 -1.964178 -11.833367 -100.941141
6 6.000000 -6.562500 8.945861 -2.522462 -13.081185 -94.488527
7 7.000000 -7.230903 9.596439 -2.957906 -14.180211 -88.708034
8 8.000000 -7.777778 10.185964 -3.280939 -15.093523 -83.633631
9 9.000000 -8.203125 10.712644 -3.505588 -15.805504 -79.267662
10 10.000000 -8.506944 11.180897 -3.648397 -16.321003 -75.558850
11 11.000000 -8.689236 11.603201 -3.725755 -16.659970 -72.392050
12 12.000000 -8.750000 12.000000 -3.750000 -16.845543 -69.600878
13 13.000000 -8.689236 12.396799 -3.725755 -16.889440 -67.003199
14 14.000000 -8.506944 12.819103 -3.648397 -16.782985 -64.443028
15 15.000000 -8.203125 13.287356 -3.505588 -16.499460 -61.816277
16 16.000000 -7.777778 13.814036 -3.280939 -16.005878 -59.069041
17 17.000000 -7.230903 14.403561 -2.957906 -15.277444 -56.174060
18 18.000000 -6.562500 15.054139 -2.522462 -14.309495 -53.098717
19 19.000000 -5.772569 15.760223 -1.964178 -13.126180 -49.773973
20 20.000000 -4.861111 16.514608 -1.276137 -11.788732 -46.064496
21 21.000000 -3.828125 17.309856 -0.454279 -10.409278 -41.729113
22 22.000000 -2.673611 18.139075 0.503378 -9.184425 -36.337384
23 23.000000 -1.397569 18.996259 1.597437 -8.503984 -29.006402
24 24.000000 0.000000 19.876356 2.827642 -9.577076 -16.878208
Those are some picture (thanks to excell) at different positions:
The slope of a parabola ax^2+bx+c is 2ax+b.
To find where the circle has a certain slope value (s), you have y/x = s. Together with the equation x^2+y^2=r^2 (you probably want the lower half of the circle, btw), you can see where, relative to the center of the circle, the circle's edge will touch the parabola.
So if the distance between the circle center and the parabola point is different from the distance between the circle center and the tangent point, you need to move the axis.
I'm working on some 3d fractals... If I take any arbitrary point (x,y,z), start from there, and then draw a line of given length "d", in a direction defined by Euler angles... (by rotation A about the x-axis, B about the y-axis, and C about the z-axis) -- and then calculate the resulting endpoint of the line.
This would be simple in 2 dimensions, as I could find the endpoint like:
endX = beginX + d * cos(angle)
endY = beginY + d * sin(angle)
Basically, I need to fill in the blanks here:
endX = beginX + d * (??)
endY = beginY + d * (??)
endZ = beginZ + d * (??)
Where I only know angles defined by 3 rotations, 1 about each axis
I was reading the article here: http://paulbourke.net/geometry/polygonise/.
Currently, I have a minecraft like terrain generated using simplex noise which I divided into 16x16 chunks that has a 32x32x128 blocks. Now, I want to use the noise I generated to the Polygonise function in marching cubes. But my problem is how can I compute the isovalue? I don't get it.
Anyone here knows a much more spoonfeed article. lol
EDIT:
Hey I found this on http://paulbourke.net/geometry/polygonise/marchingsource.cpp.
sSourcePoint[] has a value of 0.5, so it just center the object but what does the fResult += 0.5/(fDx*fDx + fDy*fDy + fDz*fDz) does? Omg, I'm intimidated with the codes.
GLfloat fSample1(GLfloat fX, GLfloat fY, GLfloat fZ)
{
GLdouble fResult = 0.0;
GLdouble fDx, fDy, fDz;
fDx = fX - sSourcePoint[0].fX;
fDy = fY - sSourcePoint[0].fY;
fDz = fZ - sSourcePoint[0].fZ;
fResult += 0.5/(fDx*fDx + fDy*fDy + fDz*fDz);
fDx = fX - sSourcePoint[1].fX;
fDy = fY - sSourcePoint[1].fY;
fDz = fZ - sSourcePoint[1].fZ;
fResult += 1.0/(fDx*fDx + fDy*fDy + fDz*fDz);
fDx = fX - sSourcePoint[2].fX;
fDy = fY - sSourcePoint[2].fY;
fDz = fZ - sSourcePoint[2].fZ;
fResult += 1.5/(fDx*fDx + fDy*fDy + fDz*fDz);
return fResult;
}
Well, Paul's source is really "spoonfed". The comment before fSample1 says:
//fSample1 finds the distance of (fX, fY, fZ) from three moving points
Basically, he is creating a so called "metaballs" object, thus he needs to "blend" three distance functions (distances from fSourcePoint[i]) into one. To do so he takes the
Isovalue = 1/f[0] + 1/f[1] + 1/f[2]
where
f[i] = 1/DistFromCenterToSourcePoint[i].
The effect is simple - when you are far from each three points, the isovalue is almost zero. The closer to the point - the lesser the f[i] and the greater your isovalue.
The distance is a usual squared euclidean distance
dist(p1, p2) = sqrt( (p1.x - p2.x)^2 + (p1.y - p2.y)^2 + (p1.z - p2.z)^2)
To achieve "Minecraft-like" isosurfaces you need to use some other metric. Take a look at taxicab (a.k.a. Manhattan) metric:
dist1(p1, p2) = abs(p1.x - p2.x) + abs(p1.y - p2.y) + abs(p1.z - p2.z)
or the max-metric
distMax(p1, p2) = max( abs(p1.x - p2.x), abs(p1.y - p2.y), abs(p1.z - p2.z) )
The "spheres" in these metrics (i.e., the sets satisfying the sphere's equation "dist = R") are cubes.
Invert them, calculate the sum (do it all in the fSample1 function), choose some typical isovalue with experiments and see the result.