码迷,mamicode.com
首页 > 编程语言 > 详细

图形填充之边标志算法

时间:2017-05-24 00:47:51      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:分配   int   填充算法   image   cpp   最小值   get   lease   point   

编译器:VS2013

前言:该算法是我所写的四个算法里最难的,也是有问题的一个,问题在于特殊情况时,总会多出现一条直线,一直再想办法避免,但还是没想出来。。。。。。。。

算法述论:

技术分享

 

源码:

  1 // 边标志填充算法.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include<stdio.h>
  6 #include"graphics.h"
  7 #include<stdlib.h>
  8 
  9 void dealpoint(int a[], int n);
 10 int maxcoor(int a[], int n, int x);
 11 int mincoor(int a[], int n, int x);
 12 void fill(int a[], int maxx, int minx, int miny, int maxy);
 13 
 14 int main()
 15 {
 16     int gdriver = DETECT, gmove,n,i,maxx,minx,maxy,miny;
 17 
 18     printf("please input the number of coordinate:\n");
 19     scanf_s("%d", &n);
 20 
 21     int *a = (int *)malloc((n + 2)*sizeof(int));//动态分配内存,多分配两个存储起点
 22 
 23     printf("please input the coordinate:\n");
 24     for (i = 0; i < n + 2; i++)
 25         scanf_s("%d", &a[i]);
 26 
 27     //利用外接矩形缩小范围
 28     maxx = maxcoor(a, n, 0);
 29     minx = mincoor(a, n, 0);
 30     maxy = maxcoor(a, n, 1);
 31     miny = mincoor(a, n, 1);
 32 
 33     initgraph(&gdriver, &gmove, "");
 34 
 35     setcolor(YELLOW);//设置画笔颜色
 36 
 37     drawpoly(n / 2 + 1, a);
 38     dealpoint(a, n);
 39     fill(a, maxx, minx, miny, maxy);
 40 
 41     system("pause");
 42 
 43     closegraph();
 44 
 45     return 0;
 46 }
 47 
 48 //检查顶点是否为区域最大点或最小点
 49 void dealpoint(int a[], int n)
 50 {
 51     int i, j, k, x ,y, color[6] = { 0, 0, 0, 0, 0, 0 };
 52 
 53     for (i = 0; i < n; i += 2)//遍历点的数组
 54     {
 55         //遍历顶点上方和下方三个点
 56         for (j = -1,x=0; j <= 1; j += 2)
 57         for (k = -1; k <= 1; k++,x++)
 58         {
 59             if (getpixel(a[i] + k, a[i + 1] + j) == YELLOW)
 60                 color[x] = 1;
 61         }
 62 
 63         //如果该点位区域最大点或最小点,则将该点变成背景色
 64         if (!((color[0] || color[1] || color[2]) && (color[3] || color[4] || color[5])))
 65             putpixel(a[i], a[i + 1], BLACK);
 66 
 67         for (y = 0; y < 6; y++)
 68             color[y] = 0;
 69     }
 70 }
 71 
 72 //填充颜色
 73 void fill(int a[], int maxx, int minx, int miny, int maxy)
 74 {
 75     int temp ,i,j;
 76 
 77     for (j = miny; j <= maxy; j++)
 78     {
 79         temp = 0;
 80 
 81         for (i = minx; i <= maxx; i++)
 82         {
 83             if ((getpixel(i, j) == YELLOW) && (getpixel(i + 1, j) == BLACK))
 84                 temp++;
 85 
 86             if ((temp % 2) == 1)
 87                 putpixel(i, j, YELLOW);
 88         }
 89     }
 90 
 91 }
 92 
 93 //返回最大值
 94 int maxcoor(int a[], int n,int x)
 95 {
 96     int i,max=a[0];
 97 
 98     for (i = x; i < n; i+=2)
 99         if (a[i]>max)
100             max = a[i];
101 
102     return max;
103 }
104 
105 //返回最小值
106 int mincoor(int a[], int n, int x)
107 {
108     int i, min = a[0];
109 
110     for (i = x; i < n; i += 2)
111         if (a[i]<min)
112             min = a[i];
113 
114     return min;
115 }

 

结果:

技术分享

产生该直线的原因并非是顶点,而是顶点附近的点,一直没想到办法避免

下面是简单的菱形

技术分享

 

图形填充之边标志算法

标签:分配   int   填充算法   image   cpp   最小值   get   lease   point   

原文地址:http://www.cnblogs.com/cdp1591652208/p/6896765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!