提问人:erkan2541 提问时间:10/22/2023 最后编辑:Clifforderkan2541 更新时间:10/26/2023 访问量:149
如何在 c 编程中找到用 BGI 绘制的四边形的面积
How can I find the area of a quadrilateral drawn with BGI in c programming
问:
如何找到用 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);
}
此代码计算不正确
答:
0赞
greg spears
10/26/2023
#1
这是一种解决方案,用于查找在图形模式下在屏幕上绘制的特定(三角形、四边形和五边形)(每个 OP))几何对象的区域。它读取屏幕像素以辨别物体的确切位置和一般性质/形状,并最终辨别其面积。
测试对象:三角形、矩形、四边形、五边形、圆形、无/空。此处显示了使用可运行代码进行概念验证的测试平台。
逻辑:假设所有未上漆的屏幕像素均为零(黑色)。我们从左到右和从上到下遍历屏幕剪辑区域,检查每个像素。如果我们遇到一个非黑色(非零)像素,假设我们已经找到了对象的开始(或关闭)边缘。根据这些发现的边缘计算每行的宽度。将所有行宽相加为总和。
当我们遍历整个剪裁区域后,我们将此总和作为对象区域返回。
此代码适用于 Turbo C -- 根据 OP 的引用:“在”BGI“(Borland 图形接口)中绘制。但是,代码的逻辑是可移植的,因此我们只需要将 getpixel() 替换为适用于任何平台的 API。
typedef struct tagRECT
{
int left, top, right, bottom;
}RECT;
/*-------------------------------------------------------------------------------
get_object_area()
Param: Rectangle structure having left, top, right and bottom coordinates
of the clipping region. The screen object must reside fully within this
bounding rectangle.
Returns: area (in pixels) of screen object, zero if no object found.
NOTES:
Includes object edges in calculation of area.
USES: Turbo C's getpixel() (adjust for your platform). This should compile
in Turbo C without any modification. Be sure to #inc1ude <graphics.h>
--------------------------------------------------------------------------------*/
int get_object_area(RECT region)
{
int x, y, color;
int start_x, end_x;
int sum = 0;
for(y=region.top; y <= region.bottom; y++) /* From top to bottom of clipping region */
{
start_x = -1, end_x = -2; /* reset for each region row */
for(x = region.left; x <= region.right; x++) /* From left to right of clipping region*/
{
/* Examine the color returned by getpixel() for every pixel. */
color = getpixel(x, y);
if(color)
{
if(start_x < 0) /* Found left edge yet?*/
{
start_x = end_x = x; /* Found it now. Assign values. */
}
else
{
end_x = x; /* Have left edge already. Just update for right edge */
}
}
} /* Go get next screen row */
sum += ( end_x - start_x + 1 ); /* Add each row width to sum */
}
/* Return sum of pixels this object( area ): */
return sum;
}
评论
1赞
Eric Postpischil
10/26/2023
考虑一个左边形,该多边形是一条水平多于垂直的对角线。此线的渲染将在同一行中包含两个相邻像素。对于第一个,此答案中的代码将设置为 1 并递增为 1。对于第二个,由于不为零,它将设置为 0。然后,即使以下像素位于多边形内,代码也不会计算它们。inside_object
edge_count
edge_count
inside_object
inside_object
0赞
greg spears
10/26/2023
@Eric Postpischil - 你能够找到一个失败的测试用例,这让我有点害怕,哈哈。但你是对的!我将努力缩小这个差距,上帝保佑,在我发布之前找到另一个可能失败的测试用例。一如既往地感谢,埃里克!
0赞
greg spears
10/26/2023
@Eric Postpischil -- 根据您的发现和善意的评论进行更新。现在肯定是金色的吗?嘿嘿嘿
上一个:重力形式 UPDAT 计算
评论