# 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
2

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

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

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

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.

Official site

GitHub

Wiki

Documentation