Closed Bezier curve - c++

I implemented a Bezier curve drawing function like this:
Vector Bezier(float t)
{
Vector rt(0,0);
int n = length-1;
for(int i=0;i<length;i++)
{
float Bi = 1;
for(int j = 1;j<=i;j++)
{
Bi *= (float) (n-j+1)/j;
}
Bi *= pow(t,i) * pow(1-t, n-i);
rt = rt + (Cpoints[i] * Bi);
}
return rt;
}
void drawBezier()
{
int segments = 100;
glBegin( GL_LINE_STRIP );
for(int i=0;i<segments;i++)
{
float t = (float) i / segments;
Vector p = Bezier(t);
glVertex2f(p.x, p.y);
}
glEnd( );
}
CPoints is an array containing the coordinates of the Control Points, length is the number of Control Points. The question is, how do I make it a closed Bezier curve, like this:

Simply use an additional segment that connects the last endpoint to the first (ex: duplicating the first control point).
A single bezier spline segment, whether it's cubic or quadratic or quartic, can't represent that kind of closed shape. Yet multiple segments can.
So you typically don't want to modify your multi-segment curve drawing function, per se, but rather the control points you feed into it. Although you could modify the drawing function to accept a flag to draw a closing segment, it's probably easier is to just think of it as a problem associated with the control points/curve segments you provide as input.

Related

Rendering line-arcs polylines with windows GDI

I have to render on screen a sequence of 2D lines and circular arcs (constant radius) connected to each other, forming a continuous path (the tangent may still have discontinuities, so it's just C0). This is a typical task in CNC machinery GUIs.
Each element of the path can be generalized as:
struct Gline
{
int x,y; // [pix] Destination point
double bulge; // Normalized sagitta (0:straight-line, 1:half-circle)
void get_center_and_radius(int& xc, int& yc, int& r) const; // Makes sense just for arcs (bulge not zero)
};
where bulge (tan(arc_angle_span/4)) represents somewhat the "curviness" of the line, if zero the line is straight, if one the line is a semicircle, ecc...
From these data (actually is needed also the starting point, but that's not important in this context) you can calculate the center and radius of the arc.
My current solution uses the windows GDI functions LineTo and Arc:
const double toll = 1E-6;
// HDC hdc; // Device context
int x_from = x_start, // The starting point
y_from = y_start;
::MoveTo(hdc, x_from, y_from);
std::vector<Gline> gpolyline;
for( const Gline& gline : gpolyline )
{
if( std::fabs(gline.bulge) < toll )
{// Straight line
::MoveTo(hdc, x_from, y_from);
::LineTo(hdc, gline.x, gline.y);
}
else
{// Arc
int xc=0, yc=0; // [pix] Center coordinates
int r = 0; // [pix] Radius
gline.get_center_and_radius(xc,yc,r);
if( gline.bulge > toll )
{// Positive arc
::Arc(hdc, xc-r, yc-r, xc+r, yc+r, x_from, y_from, gline.x, gline.y);
}
else
{// Negative arc
::Arc(hdc, xc-r, yc-r, xc+r, yc+r, gline.x, gline.y, x_from, y_from);
}
}
x_from = gline.x;
y_from = gline.y;
}
This somewhat works, but the problem with this approach is that lines and arcs aren't really connected, there's no constraint that indicates that the destination point of a line/arc is coincident with the starting point of the next arc.
This becomes apparent when zooming the viewport too much such as the numeric errors in get_center_and_radius function become painfully visible.
I was looking for a PolyLine/PolyDraw like function for lines and arcs, I'm wondering if there's a smart way to adapt PolyDraw to approximate arcs but I'm not sure if it can be done or would be worth it.
I'm seeking advice if my solution can be improved in the Windows GDI realm, how others may have approached this, or if I just have to use an external graphic library.

cocos2dx detect intersection with polygon sprite

I am using cocos2d-x 3.8.
I try to create two polygon sprites with the following code.
I know we can detect intersect with BoundingBox but is too rough.
Also, I know we can use Cocos2d-x C++ Physics engine to detect collisions but doesn't it waste a lot of resource of the mobile device? The game I am developing does not need physics engine.
is there a way to detect the intersect of polygon sprites?
Thank you.
auto pinfoTree = AutoPolygon::generatePolygon("Tree.png");
auto treeSprite= Sprite::create(pinfoTree);
treeSprite-> setPosition(width / 4 * 3 - 30 , height / 2 - 200);
this->addChild(treeSprite);
auto pinfoBird = AutoPolygon::generatePolygon("Bird.png");
auto Bird= Sprite::create(pinfoTree);
Bird->setPosition(width / 4 * 3, height / 2);
this->addChild(Bird)
This is a bit more complicated: AutoPolygon gives you a bunch of triangles - the PhysicsBody::createPolygon requires a convex polygon with clockwise winding… so these are 2 different things. The vertex count might even be limited. I think Box2d’s maximum count for 1 polygon is 8.
If you want to try this you’ll have to merge the triangles to form polygons. An option would be to start with one triangle and add more as long as the whole thing stays convex. If you can’t add any more triangles start a new polygon. Add all the polygons as PhysicsShapes to your physics body to form a compound object.
I would propose that you don’t follow this path because
Autopolygon is optimized for rendering - not for best fitting
physics - that is a difference. A polygon traced with Autopolygon will always be bigger than the original sprite - Otherwise you would see rendering artifacts.
You have close to no control over the generated polygons
Tracing the shape in the app will increase your startup time
Triangle meshes and physics outlines are 2 different things
I would try some different approach: Generate the collision shapes offline. This gives you a bunch of advantages:
You can generate and tweak the polygons in a visual editor e.g. by
using PhysicsEditor
Loading the prepares polygons is way faster
You can set additional parameters like mass etc
The solution is battle proven and works out of the box
But if you want to know how polygon intersect work. You can look at this code.
// Calculate the projection of a polygon on an axis
// and returns it as a [min, max] interval
public void ProjectPolygon(Vector axis, Polygon polygon, ref float min, ref float max) {
// To project a point on an axis use the dot product
float dotProduct = axis.DotProduct(polygon.Points[0]);
min = dotProduct;
max = dotProduct;
for (int i = 0; i < polygon.Points.Count; i++) {
flaot d = polygon.Points[i].DotProduct(axis);
if (d < min) {
min = dotProduct;
} else {
if (dotProduct> max) {
max = dotProduct;
}
}
}
}
// Calculate the distance between [minA, maxA] and [minB, maxB]
// The distance will be negative if the intervals overlap
public float IntervalDistance(float minA, float maxA, float minB, float maxB) {
if (minA < minB) {
return minB - maxA;
} else {
return minA - maxB;
}
}
// Check if polygon A is going to collide with polygon B.
public boolean PolygonCollision(Polygon polygonA, Polygon polygonB) {
boolean result = true;
int edgeCountA = polygonA.Edges.Count;
int edgeCountB = polygonB.Edges.Count;
float minIntervalDistance = float.PositiveInfinity;
Vector edge;
// Loop through all the edges of both polygons
for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++) {
if (edgeIndex < edgeCountA) {
edge = polygonA.Edges[edgeIndex];
} else {
edge = polygonB.Edges[edgeIndex - edgeCountA];
}
// ===== Find if the polygons are currently intersecting =====
// Find the axis perpendicular to the current edge
Vector axis = new Vector(-edge.Y, edge.X);
axis.Normalize();
// Find the projection of the polygon on the current axis
float minA = 0; float minB = 0; float maxA = 0; float maxB = 0;
ProjectPolygon(axis, polygonA, ref minA, ref maxA);
ProjectPolygon(axis, polygonB, ref minB, ref maxB);
// Check if the polygon projections are currentlty intersecting
if (IntervalDistance(minA, maxA, minB, maxB) > 0)
result = false;
return result;
}
}
The function can be used this way
boolean result = PolygonCollision(polygonA, polygonB);
I once had to program a collision detection algorithm where a ball was to collide with a rotating polygon obstacle. In my case the obstacles where arcs with certain thickness. and where moving around an origin. Basically it was rotating in an orbit. The ball was also rotating around an orbit about the same origin. It can move between orbits. To check the collision I had to just check if the balls angle with respect to the origin was between the lower and upper bound angles of the arc obstacle and check if the ball and the obstacle where in the same orbit.
In other words I used the various constrains and properties of the objects involved in the collision to make it more efficient. So use properties of your objects to cause the collision. Try using a similar approach depending on your objects

Marching Cubes Issues

I've been trying to implement the marching cubes algorithm with C++ and Qt. Anyway, so far all the steps have been written, but I'm getting a really bad result. I'm looking for orientation or advices about what can be going wrong. I suspect one of the problems may be with the voxel conception, specifically about which vertex goes in which corner (0, 1, ..., 7). Also, I'm not a 100% sure about how to interpret the input for the algorithm (I'm using datasets). Should I read it in the ZYX order and move the marching cube in the same way or it doesn't matter at all? (Leaving aside the fact that no every dimension has to have the same size).
Here is what I'm getting against what it should look like...
http://i57.tinypic.com/2nb7g46.jpg
http://en.wikipedia.org/wiki/Marching_cubes
http://en.wikipedia.org/wiki/Marching_cubes#External_links
Paul Bourke. "Overview and source code".
http://paulbourke.net/geometry/polygonise/
Qt_MARCHING_CUBES.zip: Qt/OpenGL example courtesy Dr. Klaus Miltenberger.
http://paulbourke.net/geometry/polygonise/Qt_MARCHING_CUBES.zip
The example requires boost, but looks like it probably should work.
In his example, it has in marchingcubes.cpp, a few different methods for calculating the marching cubes: vMarchCube1 and vMarchCube2.
In the comments it says vMarchCube2 performs the Marching Tetrahedrons algorithm on a single cube by making six calls to vMarchTetrahedron.
Below is the source for the first one vMarchCube1:
//vMarchCube1 performs the Marching Cubes algorithm on a single cube
GLvoid GL_Widget::vMarchCube1(const GLfloat &fX, const GLfloat &fY, const GLfloat &fZ, const GLfloat &fScale, const GLfloat &fTv)
{
GLint iCorner, iVertex, iVertexTest, iEdge, iTriangle, iFlagIndex, iEdgeFlags;
GLfloat fOffset;
GLvector sColor;
GLfloat afCubeValue[8];
GLvector asEdgeVertex[12];
GLvector asEdgeNorm[12];
//Make a local copy of the values at the cube's corners
for(iVertex = 0; iVertex < 8; iVertex++)
{
afCubeValue[iVertex] = (this->*fSample)(fX + a2fVertexOffset[iVertex][0]*fScale,fY + a2fVertexOffset[iVertex][1]*fScale,fZ + a2fVertexOffset[iVertex][2]*fScale);
}
//Find which vertices are inside of the surface and which are outside
iFlagIndex = 0;
for(iVertexTest = 0; iVertexTest < 8; iVertexTest++)
{
if(afCubeValue[iVertexTest] <= fTv) iFlagIndex |= 1<<iVertexTest;
}
//Find which edges are intersected by the surface
iEdgeFlags = aiCubeEdgeFlags[iFlagIndex];
//If the cube is entirely inside or outside of the surface, then there will be no intersections
if(iEdgeFlags == 0)
{
return;
}
//Find the point of intersection of the surface with each edge
//Then find the normal to the surface at those points
for(iEdge = 0; iEdge < 12; iEdge++)
{
//if there is an intersection on this edge
if(iEdgeFlags & (1<<iEdge))
{
fOffset = fGetOffset(afCubeValue[ a2iEdgeConnection[iEdge][0] ],afCubeValue[ a2iEdgeConnection[iEdge][1] ], fTv);
asEdgeVertex[iEdge].fX = fX + (a2fVertexOffset[ a2iEdgeConnection[iEdge][0] ][0] + fOffset * a2fEdgeDirection[iEdge][0]) * fScale;
asEdgeVertex[iEdge].fY = fY + (a2fVertexOffset[ a2iEdgeConnection[iEdge][0] ][1] + fOffset * a2fEdgeDirection[iEdge][1]) * fScale;
asEdgeVertex[iEdge].fZ = fZ + (a2fVertexOffset[ a2iEdgeConnection[iEdge][0] ][2] + fOffset * a2fEdgeDirection[iEdge][2]) * fScale;
vGetNormal(asEdgeNorm[iEdge], asEdgeVertex[iEdge].fX, asEdgeVertex[iEdge].fY, asEdgeVertex[iEdge].fZ);
}
}
//Draw the triangles that were found. There can be up to five per cube
for(iTriangle = 0; iTriangle < 5; iTriangle++)
{
if(a2iTriangleConnectionTable[iFlagIndex][3*iTriangle] < 0) break;
for(iCorner = 0; iCorner < 3; iCorner++)
{
iVertex = a2iTriangleConnectionTable[iFlagIndex][3*iTriangle+iCorner];
vGetColor(sColor, asEdgeVertex[iVertex], asEdgeNorm[iVertex]);
glColor4f(sColor.fX, sColor.fY, sColor.fZ, 0.6);
glNormal3f(asEdgeNorm[iVertex].fX, asEdgeNorm[iVertex].fY, asEdgeNorm[iVertex].fZ);
glVertex3f(asEdgeVertex[iVertex].fX, asEdgeVertex[iVertex].fY, asEdgeVertex[iVertex].fZ);
}
}
}
UPDATE: Github working example, tested
https://github.com/peteristhegreat/qt-marching-cubes
Hope that helps.
Finally, I found what was wrong.
I use a VBO indexer class to reduce the ammount of duplicated vertices and make the render faster. This class is implemented with a std::map to find and discard already existing vertices, using a tuple of < vec3, unsigned short >. As you may imagine, a marching cubes algorithm generates structures with thousands if not millions of vertices. The highest number a common unsigned short can hold is 65536, or 2^16. So, when the output geometry had more than that, the map index started to overflow and the result was a mess, since it started to overwrite vertices with the new ones. I just changed my implementation to draw with common VBO and not indexed while I fix my class to support millions of vertices.
The result, with some minor vertex normal issues, speaks for itself:
http://i61.tinypic.com/fep2t3.jpg

OpenGL-How do I draw randomly non-overlapping circle

I've already draw few random placed circle, but I want different numbers of circles every time I run the program. So the question really is how do I call a function in random times?
I also need them to have convex and concave effects but unable to arrange them to random circles.
Finally, I need circles not to overlap, how do I do that?
Below is my partial code so far.
void circle(){
double r=50;
int dx, dy;
dx=rand()%350;
dy=rand()%250;
glLineWidth(1);
glEnable(GL_LINE_SMOOTH);
glBegin(GL_LINES);
double i=1.0;
double j=0.0;
for (double y=r; y>=-r; y=y-1) {
i=i-0.01;
j=j+0.01;
//glColor3f(i, i, i);
glColor3f(j, j, j);
glVertex2f(-sqrt(r*r-y*y)+dx, y+dy);
glVertex2f(sqrt(r*r-y*y)+dx, y+dy);
}
glEnd();
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
srand((unsigned)time(0));
circle();
circle();
circle();
glEnd();
glFlush();
}
The simple approach
It is enough to pick a random number and iterate. This can cause the circles to overlap.
int min_circles = 10;
int max_circles = 100;
int amount = min_circles + rand()%(max_circles-min_circles+1);
for (int i = 0; i < amount; i++)
circle();
Non overlapping
This is not a trivial matter and has been an interesting research topic. You can have several approaches to this problem for example:
Naïve Generation
Store all previously added circle positions and if you try to add a new one just do a check with the previous for overlapping. If it overlaps choose another position. If it fails several times in a row then stop.
Easing
You can also consider treating all circles as a system of points connected with springs and iterate a few times so that the springs will repel the circles from each other thus conforming to the non-overlapping rule.
Poisson Disk Sampling
You can read up on implementing Poisson Disk Sampling on the internet. The algorithm generates a uniform random distribution of points on a plane so that they are at certain distance from each other. Then just use these points as centers of circles.
how do I call a function in random times?
that one way of doing it:
if (rand() % someNumber == 0)
callFunc();
I also need them to have convex and concave effects but unable to arrange them to random circles.
i didnt understand what you mean.
Finally, I need circles not to overlap, how do I do that?
first you need to creaye circles in a structure, and store them in a collection. assume you the structure like that:
struct Circle {
float x, y, raduis;
}
and a collection like that:
std::vector<Circle> circles;
then the creation may look like that:
Circle c;
do {
bool canExit = true;
c.x = rand()*350;
c.y = rand()*250;
c.radius = 50;
for (int i = 0; i < circles.size(); i++) {
float dx = (circles[i].x - c.x);
float dy = (circles[i].y - c.y);
floar radii = circles[i].radius + c.radius;
if (dx*dx + dy*dy < (radii*radii) ) {
canExit = false;
break;
}
}
while(canExit == false);
circles.push_back(c);
of cours thats only may way, hope it works for you, and good luck.

Drawing a crescent shape in OpenGL

How can I draw a 2D crescent or moon shape in OpenGL? I have tried using sin and cos like how I did for drawing circles but because a crescent has a "cut" inside it, the sin and cos don't look enough. I couldn't figure out how I could do an intersection between 2 polygons either. So I'm thinking if there a mathematical formula for drawing the crescent?
This isn't mathematically correct, but it may be close enough to meet your needs:
void drawCrescentLine(float step,float scale,float fullness) {
float angle=0.0f;
while (angle<M_PI) {
glVertex2f(scale*sinf(angle),scale*cosf(angle));
angle+=step;
}
while (angle<(2.0f*M_PI)) {
glVertex2f(fullness*scale*sinf(angle),scale*cosf(angle));
angle+=step;
}
glVertex2f(0.0f,scale);
}
or
void drawCrescentTriStrip(float step,float scale,float fullness) {
glVertex2f(0.0f,scale);
float angle=step;
while (angle<M_PI) {
float sinAngle=sinf(angle);
float cosAngle=cosf(angle);
glVertex2f(scale*sinAngle,scale*cosAngle);
glVertex2f(-fullness*scale*sinAngle,scale*cosAngle);
angle+=step;
}
glVertex2f(0.0f,-scale);
}
At fullness=1, it will draw a circle of size scale while at fullness=-0.99f, it will draw a very thin cresent. You could use two different fullness values, rightFullness and leftFullness, and always set one of them to 1.0f so you can change the direction of the crescent.
You can draw two perpendicular ellipses that intersect each other. A crescent is formed with the space that is cut out from one of the eclipses. The intersection can be removed by using a bitwise NAND logical operator when drawing.
glEnable(GL_COLOR_LOGIC_OP);
drawEllipse1();
glLogicOp(GL_NAND);
drawEllipse2();
The long way of doing it is to specify a bunch of vertices that form a skeleton for the shape that you want. You can then 'connect the dots' with GL_LINES to draw your shape. If you want a smoother shape, you can use the vertices as control points for a Bezier/Catmull-Rom spline that would draw a smooth curve joining all your vertices.
You can try this:
Vertex outside [N+1]; // Fill in N with the precision you want
Vertex inside [N+1]; // Fill in N with the precision you want
double neg_size = sqrt (1 + NEG_DIST); // Size of intescting circle.
// NEG_DIST is the distance between their centers
// Greater NEG_DIST => wider crecent
double start_angle = atan (1 / NEG_DIST); // Start angle for the inside edge
double arc = M_PI - (2 * start_angle); // Arc of the inside edge
for (int i = 0; i <= N; i++)
{
// Outside edge
outside [i].x = cos ((M_PI / N) * i) * SIZE;
outside [i].y = sin ((M_PI / N) * i) * SIZE;
// Inside edge
inside [i].x = (cos (start_angle + ((arc / N) * i)) * neg_size) * SIZE;
inside [i].y = (sin (start_angle + ((arc / N) * i)) * neg_size - NEG_DIST) * SIZE;
}
This produces the intersected polys version of a crescent. It will give you an array of coordinates for an inside and outside arc for a crescent. Then you can feed these through your favorite draw method.
NOTE: The endpoints of inside and outside overlap (I did this so that I wouldn't have +/- 1's all over the place). I'm pretty sure a GL program will be fine with it, but if you have a fence post error with this, that may be where it came from