如何在c编程中求BGI画的四边形的面积

问题描述 投票:0回答:1

如何通过BGI绘制的形状(三角形、四边形、五边形),在C编程中给定坐标点,通过将四边形内的单位正方形相加并通过边线来求其面积?

我用图中给出的坐标点在BGI中绘制了它,但是我需要通过添加边缘线经过的单位正方形来找到它的面积。我的项目需要这个。

void dortgen_alani(int sekil_ciz[][2])
{
int x[4], y[4];

    for (int i = 0; i < 4; i++) {
        x[i]=sekil_ciz[i][0];
        y[i]=sekil_ciz[i][1];
        
        
        putpixel(x[i], y[i], WHITE); // Noktaları işaretle
    }


    int toplamBirimKare = 0;

    int minX = x[0], minY = y[0];
    int maxX = x[0], maxY = y[0];

    for (int i = 1; i < 4; i++) {
        if (x[i] < minX) minX = x[i];
        if (x[i] > maxX) maxX = x[i];
        if (y[i] < minY) minY = y[i];
        if (y[i] > maxY) maxY = y[i];
    }
 
    for (int i = minX; i < maxX; i++) {
        for (int j = minY; j < maxY; j++) {
            int icindeMi = 1;
            for (int k = 0; k < 4; k++) {
                int x1 = x[k];
                int y1 = y[k];
                int x2 = x[(k + 1) % 4];
                int y2 = y[(k + 1) % 4];

                int xMin = x1 < x2 ? x1 : x2;
                int xMax = x1 > x2 ? x1 : x2;
               
                int yMin = y1 < y2 ? y1 : y2;
                int yMax = y1 > y2 ? y1 : y2;

                if (i >= xMin && i < xMax && j >= yMin && j < yMax) {
                    if ((i - x1) * (y2 - y1) - (j - y1) * (x2 - x1) < 0) {
                        icindeMi = 0;
                        break;
                    }
                }
            }
            if (icindeMi) {
                putpixel(i, j, BLUE);
                toplamBirimKare++;
            }
        }
        
    }

    printf("Dortgenin alani (birim karelerle): %d birim kare\n", toplamBirimKare/100);

    

}

this code does not calculate correctly
c coordinates calculation area bgi
1个回答
0
投票

这是一个用于查找屏幕上绘制的矩形面积的解决方案, 当我们对矩形的大小或位置一无所知时——我们只知道 它被绘制为除黑色之外的任何颜色。

此逻辑假设所有未绘制的像素均为零(黑色)。

逻辑:我们从左到右、从上到下遍历屏幕进行检查 每个像素。如果我们遇到一个非黑色(非零)像素,我们 假设我们已经找到形状的边缘并记录 x 和 y 边缘坐标。

当我们遍历完屏幕后,我们返回从 对象的宽度和高度,我们从保存的边缘坐标中推导出来。

此代码适用于 Turbo C——我假设您正在使用它 参考“BGI”中的绘图。然而,代码的逻辑是 可移植,因此您只需将 getpixel() 替换为适用的 API 适用于您的平台(如果不是 Turbo C)。

显示概念验证的测试床位于此处

请注意:该算法是为在矩形上成功而设计的。
其他形状检测留给用户作为练习。

/*-------------------------------------------------------------- 
    get_shape_area()

    Params:  screen width and screen height.

    Returns: area (in pixels) of shape on screen
    
    NOTES: includes shape edges in calculation of area.
    
    If you already know screen width and height, great.
    Else, you will need to use Turbo C's  detectgraph() function 
    to discover graphic mode and hence screen width and height.
    
    USES: Turbo C's getpixel()
    This should compile in Turbo C without any modification.
    #inc1ude <graphics.h> 
----------------------------------------------------------------*/
int get_shape_area(int screen_w, int screen_h)
{
int x, y, color;
int top=-1,left=-1,right=-1, bottom=-1;

    for(x=0; x < screen_w; x++)
    {
        for(y=0; y < screen_h; y++)
        {
            /* Examine the color returned by getpixel(). */ 
            color = getpixel(x, y);
            if(color)
            {
                /** Assumption:  if color is not BLACK (zero),
                ** then we have touched the edge of a shape! 
                ** Record the top, left, bottom, right coords 
                ** as we encounter them.*/
                if(top < 0)
                    top = y;
                else /* already have top, this must be bottom edge*/
                    if(y > bottom)
                        bottom = y;
                if(left < 0)
                    left = x;
                else  /*Already have left edge, this must be right edge*/
                    if(x > right)
                        right = x;
            }
        }
    }
    /* Return Width X Height ( area ): */
    return ((right-left+1) * (bottom-top+1));
}
© www.soinside.com 2019 - 2024. All rights reserved.