# Please tell me what's wrong with my code! New OpenCV User

Below is my code. It is supposed to draw lines from the center of the circle to the edge of the circle, but some of the lines are too short while others are too long. Does anyone have a fix for this?

cvNamedWindow("Shapes",CV_WINDOW_AUTOSIZE);
//Image structure

//Drawing a Rectangle
int x = shape->width;
int y = shape->height;
int m = 0;
int l = 2;
int q = 300;
int z = shape->width / l;
int a = shape->height / l;
int b = 10;
int f = -255;
int g = 255;

cvRectangle(shape,cvPoint(x,m),cvPoint(m,y),CV_RGB(m,g,m),f,g);

//Drawing a Circle
cvCircle(shape,cvPoint(z,a),q,CV_RGB(m,m,g),f);
cvCircle(shape,cvPoint(z,a),b,CV_RGB(m,g,m),f);

for(int o = 2; o < 800; o+=45){
for(int n = 2; n < 800; n +=45){
cvLine(shape, cvPoint(z,a), cvPoint(o,n), CV_RGB(0,255,0),1,8);
}
}

//Showing the image
cvShowImage("Shapes",shape);
//Escape Sequence
cvWaitKey(0);
//CleanUp
cvReleaseImage(&shape);
cvDestroyAllWindows();


}

edit retag close merge delete

1

The dimensions of the image are 600 x 600.

( 2014-07-10 11:18:32 -0500 )edit
2

Think about converting to C++ interface instead of legacy interface that you are using.

( 2014-07-10 12:08:33 -0500 )edit
1

the other answer showed you, how to get a point for a certain angle. look at that again, and make 1 loop over angles, not 2 for x and y

( 2014-07-10 12:24:52 -0500 )edit

Sort by ยป oldest newest most voted

In the following loop, you are drawing lines from circle center to arbitrary points and not to the edge of circle.

for(int o = 2; o < 800; o+=45){
for(int n = 2; n < 800; n +=45){
cvLine(shape, cvPoint(z,a), cvPoint(o,n), CV_RGB(0,255,0),1,8);
}


The distance between points (z,a) and (o,n) is not q or b. Once you fix that, you should be good.

more

@unxnut how do I find the distance between these two points?

( 2014-07-10 13:21:36 -0500 )edit

A simple way will be to get Euclidean distance as sqrt ( sqr ( x1 - x2 ) + sqr ( y1 - y2 ) ). Please also look at the answer pointed to by @berak in the comment above.

( 2014-07-11 08:53:52 -0500 )edit

Official site

GitHub

Wiki

Documentation