I've written a drawCircle function below but as more circles are drawn/redrawn, memory usage increases greatly, so I'm assuming theres a memory leak somewhere but I can't seem to figure it out. I've tried deleting the instance at the end of the function but that doesn't help.
void drawCircle(cairo_surface_t *container, int x, int y, int radius, float r, float g, float b, float a)
{
cairo_t *cairoInstance;
cairoInstance = cairo_create(container);
cairo_set_source_rgba(cairoInstance, r, g, b, a);
cairo_arc(cairoInstance, x, y, radius, 0, 2*M_PI);
cairo_stroke_preserve(cairoInstance);
cairo_fill_preserve(cairoInstance);
//delete cairoInstance;
gtk_widget_queue_draw_area(GTK_WIDGET(frame2), 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
}
Any ideas?
Thanks in advance.
A few points:
cairo_ts are reference counted. Call cairo_destroy when you are done with your cairo_t*. Alternatively, you don't need to create and destroy cairo_t's for each circle - refactor the function by pulling out the call to cairo_create.
Unless you need them, prefer the cairo_X functions to the cairo_X_preserve ones. In your code cairo_fill_preserve should probably be cairo_preserve. (The stroke should be preserved though, so that the following fill works.)
The call to invalidate a rectangle of your gtk widget with gtk_widget_queue_draw_area can be refactored out as well, and only done once per draw.
gtk_widget_queue_draw_area invalidates a rectangular region of the widgets window - you may be fine with just gdk_window_invalidate_rect - see the documentation
Psuedo-code after refactoring (with a hypothetical Circle type):
void drawCircle(cairo_t *cr, int x, int y, int radius, float r, float g, float b, float a) {
cairo_set_source_rgba(cr, r, g, b, a);
cairo_arc(cr, x, y, radius, 0, 2*M_PI);
cairo_stroke_preserve(cr); // keep the arc so that we can call cairo_fill
cairo_fill(cr);
}
void functionThatDrawsCircles(cairo_surface_t* surface, Circle* circles, int num) {
cairo_t* cr = cairo_create(surface);
for(int i = 0; i < num; i++) {
drawCircle(cr, circles[i].x, circles[i].y, 10, circles[i].r, circles[i].g, circles[i].b, 1.0);
}
cairo_destroy(cr);
gtk_widget_queue_draw_area(GTK_WIDGET(frame2), 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
}
Related
I am trying to calling Xiaolin wu's line algorithm. I find a version from rosettacode.org but I don't know how to calling it.
Here is the link:
https://rosettacode.org/wiki/Xiaolin_Wu%27s_line_algorithm#C.2B.2B
I don't understand this part
const std::function<void(int x, int y, float brightness)>& plot
Could you give me a example to calling this function.
This just means that the last argument is a function plotting a point given the coordinates and the desired brightness.
It allows different flavors, e.g. with function pointer, or lambdas.
void my_plot(int x, int y, float brightness) {
// plot things
}
int main() {
WuDrawLine(0, 0, 100, 256, my_plot);
WuDrawLine(0, 100, 0, 256, [](int x, int y, float brightness) { /*do stuff*/ });
return 0;
}
I have a function that is supposed to return true if two circles are colliding and false otherwise, and to help while developing I have also added a part within the function to draw the hitbox only when they're not colliding.
My issue is even when they are colliding it will continue to draw the hitbox, and say they're not colliding, indicating that the function isn't working properly.
int colliding(int x, int y, int r, int x1, int y1, int r1)
{
//compare the distance to combined radii
int dx = x1 - x;
int dy = y1 - y;
int radii = r + r1;
if ((dx * dx) + (dy * dy) < radii * radii)
{
return true;
}
else
{
player.hitbox.draw();
return false;
}
}
int main()
{
while (true)
{
player.draw();
int cx = 300;
int cy = 300;
int cr = 50;
al_draw_filled_circle(camera.getScreenPosX(cx), camera.getScreenPosY(cy), cr, al_map_rgb(0, 0, 0));
colliding(player.hitbox.posX, player.hitbox.posY, player.hitbox.radius, cx, cy, cr);
al_flip_display();
al_clear_to_color(al_map_rgb(255, 255, 255));
}
}
I would assume that camera.getScreenPosX/Y() transforms your cx/cy/cr circle into another space than the one where player.hitbox.posx/y are. I cannot be sure however, because implementation of player.hitbox.draw() is not given.
Your collision function seems fine, so I'd go and check whether player.hitpox.posx/y and cx/cy are in the same coordinate space.
I'm fairly new to programming and would like to know how to start implementing the following algorithm in C++,
Given a binary image where pixels with intensity 255 show edges and pixels with intensity 0 show the background, find line segments longer than n pixels in the image. t is a counter showing the number of iterations without finding a line, and tm is the maximum number of iterations allowed before exiting the program.
Let t=0.
Take two edge points randomly from the image and find equation of the line passing
through them.
Find m, the number of other edge points in the image that are within distance d pixels of
the line.
If m > n, go to Step 5.
Otherwise (m ≤ n), increment t by 1 and if t < tm go to Step 2, and
if t ≥ tm exit program.
Draw the line and remove the edge points falling within distance d pixels of it from the
image. Then, go to Step 1
Basically, I just want to randomly pick two points from the image, find the distance between them, and if that distance is too small, I would detect a line between them.
I would appreciate if a small code snippet is provided, to get me started.
this is more like a RANSAC parametric line detection. I would also keep this post updated if I get it done.
/* Display Routine */
#include "define.h"
ByteImage bimg; //A copy of the image to be viewed
int width, height; //Window dimensions
GLfloat zoomx = 1.0, zoomy = 1.0; //Pixel zoom
int win; //Window index
void resetViewer();
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
if ((w!=width) || (h!=height)) {
zoomx=(GLfloat)w/(GLfloat)bimg.nc;
zoomy=(GLfloat)h/(GLfloat)bimg.nr;
glPixelZoom(zoomx,zoomy);
}
width=w; height=h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y) {
glutPostRedisplay();
if((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN) &&
(zoomx==1.0) && (zoomy==1.0)){
printf(" row=%d, col=%d, int=%d.\n", y,x, (int)bimg.image[(bimg.nr-1-y)*bimg.nc+x]);
glutPostRedisplay();
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glDrawPixels((GLsizei)bimg.nc,(GLsizei)bimg.nr, GL_LUMINANCE,GL_UNSIGNED_BYTE, bimg.image);
glutSwapBuffers();
}
Let us assume you have an int[XDIMENSION][YDIMENSION]
Let t=0.
int t = 0; // ;-)
Take two edge points randomly from the image and find equation of the line passing through them.
Brute force: you could randomly search the image for points and re-search when they are not edge points
struct Point {
int x;
int y;
};
bool is_edge(Point a) {
return image[a.x][a.y] == 255;
}
int randomUpto(int upto) {
int r = rand() % upto;
return r;
}
, which needs the pseudo-random number generator to be initialized via
srand(time(NULL));
To find edge points
Point a;
do {
a.x = randomUpto(XDIMENSION);
a.y = randomUpto(YDIMENSION);
} while ( ! is_edge(a) );
Find m, the number of other edge points in the image that are within distance d pixels of the line.
You need the line between the points. Some searching yields this fine answer, which leads to
std::vector<Point> getLineBetween(Point a, Point b) {
double dx = b.x - a.x;
double dy = b.y - a.y;
double dist = sqrt(dx * dx + dy * dy);
dx /= dist;
dy /= dist;
std::vector<Point> points;
points.push_back(a);
for ( int i = 0 ; i < 2*dist; i++ ) {
Point tmp;
tmp.x = a.x + (int)(i * dx /2.0);
tmp.y = a.y + (int)(i * dy /2.0);
if ( tmp.x != points.back().x
|| tmp.y != points.back().y ) {
points.push_back(tmp);
}
}
return points;
}
Do you see a pattern here? Isolate the steps into substeps, ask google, look at the documentation, try out stuff until it works.
Your next steps might be to
create a distance function, euclidean should suffice
find all points next to line (or next to a point, which is easier) based on the distance function
Try out some and come back if you still need help.
I HAVE to draw a circle with the following code
(check if point is on the circle).
int rad=10;
// i=x,j=y
for (int j=ymid-rad;j<=ymid+rad;j++){
for (int i=xmid-rad;i<=xmid+rad;i++){
if (((i-xmid)*(i-xmid)+(j-ymid)*(j-ymid)) == rad*rad)
Image1->Canvas->Pixels[i][j]=clRed;
}
}
However it only draws a few points of the circle.
What am I doing wrong?
Thank you.
You're doing all of this in integer arithmetic; there are very few integer solutions to x^2 + y^2 == r^2 (for a fixed r).
I suggest using something like the midpoint circle algorithm instead.
Range based version would work like this:
bool RangeCheck(float val, float r1, float r2) {
return val >= r1 && val <= r2;
}
bool Circle(float x, float y, float rad) {
return RangeCheck(sqrtf(x*x+y*y), rad-0.8, rad+0.8);
}
bool CircleWithCenter(float x, float y, float cx, float cy, float rad) {
x-=cx; y-=cy;
return Circle(x,y,rad);
}
This kind of range is how they can draw isolines in weather forecasts, but works also for circles.
Im working on my OpenGL task, and next stage is loading models and producing shadows using shadow volumes algorithm. I do it in 3 stages -
setConnectivity - finding
neighbours of each triangle and
storing their indices in neigh
parameter of each triangle,
markVisible(float* lp) - if lp
represents vector of light's
position, it marks triangles as
visible = true or visible =
false depending on dot production
of its normal vector and light
position,
markSilhoutte(float *lp) - marking silhouette edges and building the volume itself, extending silhouette to infinity(100 units is enough) in the direction opposite to light.
I checked all stages, and can definitely say that its all ok with first two, so the problem is in third function, which i included in my question. I use the algorithm introduced in this tutorial: http://www.3dcodingtutorial.com/Shadows/Shadow-Volumes.html
Briefly, edge is included in silhouette if it belongs to the visible triangle and non-visible triangle at the same time.
Here is a pair of screenshots to show you whats wrong:
http://prntscr.com/17dmg , http://prntscr.com/17dmq
As you can see, green sphere represents light's position, and these ugly green-blue polygons are faces of "shadow volume". You can also see, that im applying this function to the model of cube, and one of volume's side is missing(its not closed, but i should be). Can someone suggest whats wrong with my code and how can i fix it? Here goes the code i promised to include(variables names are self-explanatory, i suppose, but if you dont think so i can add description for each of them):
void Model::markSilhouette(float* lp){
glBegin(GL_QUADS);
for ( int i = 0; i < m_numMeshes; i++ )
{
for ( int t = 0; t < m_pMeshes[i].m_numTriangles; t++ )
{
int triangleIndex = m_pMeshes[i].m_pTriangleIndices[t];
Triangle* pTri = &m_pTriangles[triangleIndex];
if (pTri->visible){
for(int j=0;j<3;j++){
int triangleIndex = m_pMeshes[i].m_pTriangleIndices[pTri->neigh[j]-1];
Triangle* pTrk = &m_pTriangles[triangleIndex];
if(!pTrk->visible){
int p1j=pTri->m_vertexIndices[j];
int p2j=pTri->m_vertexIndices[(j+1)%3];
float* v1=m_pVertices[p1j].m_location;
float* v2=m_pVertices[p2j].m_location;
float x1=m_pVertices[p1j].m_location[0];
float y1=m_pVertices[p1j].m_location[1];
float z1=m_pVertices[p1j].m_location[2];
float x2=m_pVertices[p2j].m_location[0];
float y2=m_pVertices[p2j].m_location[1];
float z2=m_pVertices[p2j].m_location[2];
t=100;
float xl1=(x1-lp[0])*t;
float yl1=(y1-lp[1])*t;
float zl1=(z1-lp[2])*t;
float xl2=(x2-lp[0])*t;
float yl2=(y2-lp[1])*t;
float zl2=(z2-lp[2])*t;
glColor3f(0,0,1);
glVertex3f(x1 + xl1,
y1 + yl1,
z1 + zl1);
glVertex3f(x1,
y1,
z1);
glColor3f(0,1,0);
glVertex3f(x2 + xl2,
y2 + yl2,
z2 + zl2);
glVertex3f(x2,
y2,
z2);
}
}
}
}
}
glEnd();
}
I've found it. It looks like if you dont see an obvious algorithm mistake for a few days, then you've made a f*cking stupid mistake.
My triangle index variable is called t. Guess what? My extending vector length is also called t, and they are in the same scope, and i set t=100 after FIRST visible triangle :D So now volumes look like this:
outside http://prntscr.com/17l3n
inside http://prntscr.com/17l40
And it looks good for all light positions(acceptable by shadow volumes aglorithm, of course). So the working code for drawing a shadow volume is the following:
void Model::markSilouette(float* lp){
glDisable(GL_LIGHTING);
glPointSize(4.0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK,GL_FILL);
glBegin(GL_QUADS);
for ( int i = 0; i < m_numMeshes; i++ )
{
for ( int t = 0; t < m_pMeshes[i].m_numTriangles; t++ )
{
int triangleIndex = m_pMeshes[i].m_pTriangleIndices[t];
Triangle* pTri = &m_pTriangles[triangleIndex];
if (pTri->visible){
for(int j=0;j<3;j++){
Triangle* pTrk;
if(pTri->neigh[j]){
int triangleIndex = m_pMeshes[i].m_pTriangleIndices[pTri->neigh[j]-1];
pTrk = &m_pTriangles[triangleIndex];
}
if((!pTri->neigh[j]) || !pTrk->visible){
int p1j=pTri->m_vertexIndices[j];
int p2j=pTri->m_vertexIndices[(j+1)%3];
float* v1=m_pVertices[p1j].m_location;
float* v2=m_pVertices[p2j].m_location;
float x1=m_pVertices[p1j].m_location[0];
float y1=m_pVertices[p1j].m_location[1];
float z1=m_pVertices[p1j].m_location[2];
float x2=m_pVertices[p2j].m_location[0];
float y2=m_pVertices[p2j].m_location[1];
float z2=m_pVertices[p2j].m_location[2];
float f=100; // THE PROBLEM WAS HERE
float xl1=(x1-lp[0])*f;
float yl1=(y1-lp[1])*f;
float zl1=(z1-lp[2])*f;
float xl2=(x2-lp[0])*f;
float yl2=(y2-lp[1])*f;
float zl2=(z2-lp[2])*f;
glColor3f(0,0,0);
glVertex3f(x1 + xl1,
y1 + yl1,
z1 + zl1);
glVertex3f(x1,
y1,
z1);
glVertex3f(x2,
y2,
z2);
glVertex3f(x2 + xl2,
y2 + yl2,
z2 + zl2);
}
}
}
}
}
glEnd();
}
I think everything is ok, you are just rendering volume without depth test =)