在c ++中绘制多边形并使用面积公式在输出中显示其面积。 [英] draw polygon in c++ and show its area in output using polygon area formula.

查看:105
本文介绍了在c ++中绘制多边形并使用面积公式在输出中显示其面积。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像我想使用graphics.h在c ++中绘制一个多边形。然后询问坐标和边。 x1,x2,y1,y2。然后绘制多边形并显示其面积和周长作为输出。使用他们的mathamatical公式。

我做到了这一点。

like i want to draw a polygon in c++ using graphics.h . then ask for coordinates and sides. x1, x2 ,y1 ,y2. then draw the polygon and show its area and perimeter as output. using their mathamatical formulas.
I have done this.

#include <iostream.h>
 #include <graphics.h>
 #include    <conio.h>
 #include     <math.h>

 void show_screen( );

 void Polygon(const int,const int []);

 void Line(const int,const int,const int,const int);

 int main( )
    {
       int driver=VGA;
       int mode=VGAHI;

       int n=0;

       do
      {
	 show_screen( );

	 gotoxy(12,13);
	 cout<<"Enter the number of points : ";
	 cin>>n;

	 int *coordinates=new int[(n*2)];

	 for(int count=0;count<n;count++)>
	{
	   gotoxy(8,18);
	   cout<<"Coordinates of Point-"<<(count+1)<<" (x"<<(count+1)<<",y"<<(count+1)<<") :";


	   gotoxy(12,21);
	   cout<<"Enter the value of x"<<(count+1)<<" = ";
	   cin>>coordinates[(count*2)];

	   gotoxy(12,22);
	   cout<<"Enter the value of y"<<(count+1)<<" = ";
	   cin>>coordinates[((count*2)+1)];

	   gotoxy(8,18);
	   cout<<"                                            ";

	   gotoxy(12,21);
	   cout<<"                                            ";

	   gotoxy(12,22);
	   cout<<"                                            ";
	 }

	 initgraph(&driver,&mode,"c:\\tc\\Bgi");

	 setcolor(15);
	   Polygon(n,coordinates);

	     delete coordinates;

	 setcolor(15);
	   outtextxy(110,460,"Press <enter> to continue or any other key to exit.");

	 int key=int(getch( ));

	 if(key!=13)
	break;
      }
       while(1);

       return 0;
    }

 void Polygon(const int n,const int coordinates[])
    {
       if(n>=2)
      {
	 Line(coordinates[0],coordinates[1],
			 coordinates[2],coordinates[3]);

	 for(int count=1;count<(n-1);count++)
	Line(coordinates[(count*2)],coordinates[((count*2)+1)],
			coordinates[((count+1)*2)],
			coordinates[(((count+1)*2)+1)]);
      }
    }

void Line(const int x_1,const int y_1,const int x_2,const int y_2)
    {
       int color=getcolor( );

       int x1=x_1;
       int y1=y_1;

       int x2=x_2;
       int y2=y_2;

       if(x_1>x_2)
      {
	 x1=x_2;
	 y1=y_2;

	 x2=x_1;
	 y2=y_1;
      }

       int dx=abs(x2-x1);
       int dy=abs(y2-y1);
       int inc_dec=((y2>=y1)?1:-1);

       if(dx>dy)
      {
	 int two_dy=(2*dy);
	 int two_dy_dx=(2*(dy-dx));
	 int p=((2*dy)-dx);

	 int x=x1;
	 int y=y1;

	 putpixel(x,y,color);

	 while(x<x2)>
	{
	   x++;

	   if(p<0)
	      p+=two_dy;

	   else
	      {
	     y+=inc_dec;
	     p+=two_dy_dx;
	      }

	   putpixel(x,y,color);
	}
      }

       else
      {
	 int two_dx=(2*dx);
	 int two_dx_dy=(2*(dx-dy));
	 int p=((2*dx)-dy);

	 int x=x1;
	 int y=y1;

	 putpixel(x,y,color);

	 while(y!=y2)
	{
	   y+=inc_dec;

	   if(p<0)
	      p+=two_dx;

	   else
	      {
	     x++;
	     p+=two_dx_dy;
	      }

	   putpixel(x,y,color);
	}
      }
    }

void show_screen( )
    {
       restorecrtmode( );
       textmode(C4350);

       textbackground(1);
       cprintf(" Polygon ");
       textbackground(8);


       gotoxy(1,2);
    }

推荐答案

假设您可以从用户那里收集输入,那么绘制多边形是微不足道的:

call
Assuming you are able collecting input from the user, then drawing the polygon is trivial:
call
moveto(x0,y0); lineto(x1,y1);...;lineto(x0,y0);



计算周长相对容易,因为例如


Computing the perimeter is relatively easy, since, for instance

l01 = sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0));



给出了连接点 0 的长度到点 1

计算该区域有点棘手,您可以在这里找到一种有趣的方法:不规则多边形区域 [ ^ ]。


gives the length of the side connecting point 0 to point 1.
Computing the area is a bit trickier, here you may find an interesting approach: "Area of Irregular Polygons"[^].


我们不做你的作业:它是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


这篇关于在c ++中绘制多边形并使用面积公式在输出中显示其面积。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆