自己把图片地址改一下
#include<opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<math.h>
#include<string.h>
using namespace cv;
void readlistanddraw(Mat image);
void savepointtolist(int x, int y);
void bresenham(Mat *image, int x0, int y0, int R, char *color, int flag1);
struct node
{
int x1, y1;
}point[2000];
int R2;
char color[10];
int count = 0;
int f = 1;
Mat image = imread("C:\\Users\\chenh\\Desktop\\earth.jpg");
int main()
{
//namedWindow("move");
int x0, y0, R;
printf("请输入大圆圆心坐标and半径:");
scanf("%d%d%d", &x0, &y0,&R);
printf("请输入颜色:");
scanf("%s",color);
printf("请输入小圆的半径:");
scanf("%d", &R2);
bresenham(&image, x0, y0, R, color, 0);
readlistanddraw(image);
}
void savepointtolist(int x,int y)
{
point[count].x1 = x;
point[count].y1 = y;
printf("%d %d\n", point[count].x1, point[count].y1);
count++;
}
void readlistanddraw(Mat image)
{
int i, j;
for (i = 0; i < 8; i++)
{
for (j = i; j < count; j = j + 8)
{
bresenham(&image, point[j].x1, point[j].y1, R2, color, 1);
imshow("move", image);
waitKey(5);
destroyAllWindows();
}
}
}
void bresenham(Mat *image,int x0,int y0,int R,char *color,int flag1)//flag1为是否填充的标志,默认为不填充,填充为1
{
int t1, t2;
int r, g, b, temp1 = x0,temp2=y0;
y0 = y0 - R;
if (strcmp("red", color) == 0)
{
r = 255, g = 255, b = 255;
}
else if (strcmp("blue", color) == 0)
{
r = 255, g = 255, b = 255;
}
else if (strcmp("green", color) == 0)
{
r = 255, g = 255, b = 255;
}
else
{
r = 255, g = 255, b = 255;
}
if(!(x0-R>=0&&image->cols-2*R>=0&&y0>=R&&image->cols-y0>=R))
{
printf("您输入不合法,请重新输入\n");
exit(1);
}
while (x0<temp1+(1.414/2)*R+1)
{
float t1 = sqrt((x0 - temp1)*(x0 - temp1) + (y0 - temp2)*(y0 - temp2));//向右移动
float t2 = sqrt((x0 - temp1)*(x0 - temp1) + (y0 -temp2-1)*(y0 - temp2-1));//右下移动
if (t1-R>0)
{
y0++;
}
//printf("%d %d\n", x0, y0);
image->at<Vec3b>(x0, y0)[0] = b;
image->at<Vec3b>(x0, y0)[1] = g;
image->at<Vec3b>(x0, y0)[2] = r;
image->at<Vec3b>(y0 - temp2 + temp1, x0 - temp1 + temp2)[0] = b;
image->at<Vec3b>(y0 - temp2 + temp1, x0 - temp1 + temp2)[1] = g;
image->at<Vec3b>(y0 - temp2 + temp1, x0 - temp1 + temp2)[2] = r;
image->at<Vec3b>(-(x0 - temp1) + temp1, y0)[0] = b;
image->at<Vec3b>(-(x0 - temp1) + temp1, y0)[1] = g;
image->at<Vec3b>(-(x0 - temp1) + temp1, y0)[2] = r;
image->at<Vec3b>(x0, -(y0 - temp2) + temp2)[0] = b;
image->at<Vec3b>(x0, -(y0 - temp2) + temp2)[1] = g;
image->at<Vec3b>(x0, -(y0 - temp2) + temp2)[2] = r;
image->at<Vec3b>(-(x0 - temp1) + temp1, -(y0 - temp2) + temp2)[0] = b;
image->at<Vec3b>(-(x0 - temp1) + temp1, -(y0 - temp2) + temp2)[1] = g;
image->at<Vec3b>(-(x0 - temp1) + temp1, -(y0 - temp2) + temp2)[2] = r;
image->at<Vec3b>(-(y0 - temp2) + temp1, x0 - temp1 + temp2)[0] = b;
image->at<Vec3b>(-(y0 - temp2) + temp1, x0 - temp1 + temp2)[1] = g;
image->at<Vec3b>(-(y0 - temp2) + temp1, x0 - temp1 + temp2)[2] = r;
image->at<Vec3b>(-(y0 - temp2) + temp1, -(x0 - temp1) + temp2)[0] = b;
image->at<Vec3b>(-(y0 - temp2) + temp1, -(x0 - temp1) + temp2)[1] = g;
image->at<Vec3b>(-(y0 - temp2) + temp1, -(x0 - temp1) + temp2)[2] = r;
image->at<Vec3b>(y0 - temp2 + temp1, -(x0 - temp1) + temp2)[0] = b;
image->at<Vec3b>(y0 - temp2 + temp1, -(x0 - temp1) + temp2)[1] = g;
image->at<Vec3b>(y0 - temp2 + temp1, -(x0 - temp1) + temp2)[2] = r;
if (!flag1)
{
savepointtolist(x0, y0);
savepointtolist(y0 - temp2 + temp1, x0 - temp1 + temp2);
savepointtolist(-(x0 - temp1) + temp1, y0);
savepointtolist(x0, -(y0 - temp2) + temp2);
savepointtolist(-(x0 - temp1) + temp1, -(y0 - temp2) + temp2);
savepointtolist(-(y0 - temp2) + temp1, x0 - temp1 + temp2);
savepointtolist(-(y0 - temp2) + temp1, -(x0 - temp1) + temp2);
savepointtolist(y0 - temp2 + temp1, -(x0 - temp1) + temp2);
}
x0++;
}
if (flag1 == 12)
{
while (sqrt((x0 - temp1)*(x0 - temp1) + (y0 + R - temp2)*(y0 + R - temp2)) <= R)
{
image->at<Vec3b>(x0, y0)[0] = b;
image->at<Vec3b>(x0, y0)[1] = g;
image->at<Vec3b>(x0, y0)[2] = r;
}
}
}