zoukankan      html  css  js  c++  java
  • 图形填充之边缘填充算法

    编译器:VS2013

    基本思想:

    基本思想:按任意顺序处理多边形的每条边。处理时,先求出该边与扫描线的交点,再对扫描线上交点右方的所有象素取补。

    取补:若该像素是背景色,则变为填充色; 若像素是填充色,则变为背景色。

    前言:刚开始接触这个算法时,一直不知道怎么找出直线上任意一点x,y的关系,困扰了很久,也和高中数学忘了差不多有关,只记得y=kx+b,然而这题使用

    (y1-y0)/(x1-x0)=(y-y0)/(x-x0)

    这样一来关系很明显表达出来,而且用k做的话会存在k不存在的情况,而这种做法极端情况是y1=y0,但是这个时候根本不用填充,好啦,老规矩,附一波源码

    代码:

     1 // 边缘填充算法.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include<stdio.h>
     6 #include"graphics.h"
     7 #include<stdlib.h>
     8 
     9 //函数声明
    10 int  maxX(int a[], int n);//求取最大值
    11 void Edgefilling(int a[], int n);
    12 int putcolor(int x, int y);//颜色填充
    13 
    14 int main()
    15 {
    16     int gdriver = DETECT, gmode,n,i;
    17     
    18     printf("please input the nymber of point:
    ");
    19     scanf_s("%d", &n);
    20 
    21     int *a = (int *)malloc(n*sizeof(int));//动态分配内存
    22     
    23     printf("input the point:
    ");
    24     for (i = 0; i < n; i++)
    25         scanf_s("%d",&a[i]);
    26 
    27     initgraph(&gdriver, &gmode, "");
    28 
    29     setcolor(YELLOW);
    30     setbkcolor(BLACK);
    31 
    32     drawpoly(n / 2, a);//画出多边形
    33 
    34     Edgefilling(a, n);
    35 
    36     system("pause");
    37     closegraph();
    38     
    39     return 0;
    40 }
    41 
    42 //边缘填充算法实现
    43 void Edgefilling(int a[],int n)
    44 {
    45     int i,x,y,Ymax,Ymin;
    46 
    47     for (i = 0; i < n - 2; i = i + 2)
    48     {
    49         //判断纵坐标大小
    50         Ymax = (a[i + 1] > a[i + 3]) ? a[i + 1] : a[i + 3];
    51         Ymin = (a[i + 1] <= a[i + 3]) ? a[i + 1] : a[i + 3];
    52 
    53         for (y = Ymin; y < Ymax; y++)
    54         {
    55             x = (y - a[i + 1])*(a[i + 2] - a[i]) / (a[i + 3] - a[i + 1]) + a[i];
    56 
    57             while (x <= maxX(a, n))
    58             {
    59                 putpixel(x, y, putcolor(x, y));
    60                 x++;
    61             }
    62         }
    63     }
    64     
    65 }
    66 
    67 //求取最大值
    68 int  maxX(int a[],int n)
    69 {
    70     int i,max=a[0];
    71 
    72     for (i = 0; i < n; i += 2)
    73     {
    74         if (a[i] > max)
    75             max = a[i];
    76     }
    77 
    78     return max;
    79 }
    80 
    81 //颜色填充
    82 int putcolor(int x, int y)
    83 {
    84     if (getpixel(x, y) == BLACK)
    85         return BLUE;
    86     else if (getpixel(x, y) == BLUE)
    87         return BLACK;
    88     else
    89         return YELLOW;
    90 }

    结果:

     

  • 相关阅读:
    Android Context 上下文 你必须知道的一切
    Delphi:对TNotifyEvent的理解
    vagrant启动报错The following SSH command responded with a no
    virtualbox命令行共享CentOS目录
    一些Linux命令
    PHP实现单例模式
    maven+springMVC+mybatis+easyUI管理用户增删改查
    Project Euler:Problem 77 Prime summations
    spring 获取对象方式
    linux命令之man和info
  • 原文地址:https://www.cnblogs.com/cdp1591652208/p/6896791.html
Copyright © 2011-2022 走看看