凸包[C程序]

[Windows下MinGW GCC下调试通过]

/*------------------------------------------------------------
   HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
                 (c) Charles Petzold, 1998
  ------------------------------------------------------------*/
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <windows.h>
#include <math.h>
//------------------------------------------------------------
#define num_dot 200
struct ddoott{float x; float y; float z;};
struct ddoott dot[num_dot];
struct ddoott tod[num_dot];
int td[num_dot];
//------------------------------------------------------------
void boarder(HDC);
void cross(HDC,int,int,int,COLORREF);
// void disptheta(HDC,float*,int,int,int,COLORREF);
void line(HDC,int,int,int,COLORREF);
float xmul(int,int,int);
// void dispinfo(HDC, float,int,int, COLORREF);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
//--------------------------------------------------------------
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("HelloWin") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("The Hello Program"), // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;

     float ix,iy,iz;
     int i;
     static int AA[100];
     char strbuf[80];
     switch (message)
     {
     case WM_CREATE:
      srand((unsigned)time(NULL));
//     PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
          for(i=0;i<num_dot;i++)
          {
            dot[i].x=50.0+rand()%400;
            dot[i].y=50.0+rand()%400;
            dot[i].z=50.0+rand()%100;
          }
          return 0 ;

     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          
          GetClientRect (hwnd, &rect) ;
          SetTextColor(hdc,RGB(255,192,192));
         // DrawText (hdc, TEXT ("Create 100 different dot..."), -1, &rect,
         //           DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          for(i=0;i<num_dot;i++)
          {	  
          // sprintf(strbuf,"%2d\0",i);
          // TextOut(hdc,dot[i].x-10,dot[i].y-10,strbuf,strlen(strbuf));
           cross(hdc,dot[i].x,dot[i].y,1,RGB(0,0,127));
          }
//	  MoveToEx(hdc,dot[0].x,dot[0].y,NULL);
//	  for(i=1;i<num_dot;i++) LineTo(hdc,dot[i].x,dot[i].y);
	  boarder(hdc);
//          for(i=0;i<num_dot;i++)
//          {	  
//           sprintf(strbuf,"%2d\0",i);
//           TextOut(hdc,dot[i].x-10,dot[i].y-10,strbuf,strlen(strbuf));
//          }
          EndPaint (hwnd, &ps) ;
          return 0 ;
          
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
//-------------------------------------------------------------
void boarder(HDC hdc)
{
  int i,j,k,p,q,r;
  int rtag,qtag,ptag;
  float low,high;
  float theta[num_dot];
  int ptheta[num_dot];
  char strbuf[80];
  low=0.0;
  for(j=i=0;i<num_dot;i++)
  {
    if(dot[i].x>low){low=dot[i].x;td[j]=i;}
  }
  cross(hdc,dot[td[j]].x,dot[td[j]].y,2,RGB(255,0,0));
  
  for(i=0;i<num_dot;i++)
  {
    if(i==td[j])theta[i]=4.0;
    else	  
    theta[i]=2+(dot[i].y-dot[td[j]].y)/sqrt((dot[i].x-dot[td[j]].x)*(dot[i].x-dot[td[j]].x)+(dot[i].y-dot[td[j]].y)*(dot[i].y-dot[td[j]].y));	  
  }
//  disptheta(hdc,theta,60,5,num_dot,RGB(255,0,0));
//sort theta[] 
  for(i=0;i<num_dot-1;i++)
  {
   k=i;
   for(j=i+1;j<num_dot;j++)
   {
     if(theta[j]<theta[k])k=j;
   }
   low=theta[k];theta[k]=theta[i];theta[i]=low;
   low=dot[k].x,dot[k].x=dot[i].x,dot[i].x=low;
   low=dot[k].y,dot[k].y=dot[i].y,dot[i].y=low;
  }

// disptheta(hdc,theta,560,5,num_dot,RGB(0,0,255));
  ptag=1;
  line(hdc,num_dot-1,0,2,RGB(255,0,0));
  line(hdc,num_dot-1,num_dot-2,2,RGB(255,128,0));  
  p=0;
  q=1;
  r=2;
  while(ptag==1)
  {
      rtag=1; 	    
      while(rtag==1)
      {
        high=xmul(p,q,r);
//        line(hdc,p,q,1,RGB(0,255,0));
//        line(hdc,p,r,1,RGB(0,0,255));
//        dispinfo(hdc,high,400+q*130,20+20*r,RGB(0,0,255));
        if(high>0.0){q=r;r=-1;rtag=0;}
        r++;
        if(r==p)r++;
        if(r==q)r++;
        if(r==num_dot-1)rtag=-1;    
      }
//      dispinfo(hdc,rtag,220,20,RGB(0,0,0));
//      dispinfo(hdc,p,500,20,RGB(0,0,0));
 //     dispinfo(hdc,q,620,20,RGB(64,0,0));
 //     dispinfo(hdc,r,740,20,RGB(0,64,0));
      if(rtag==-1)
      {
      line(hdc,p,q,2,RGB(255,0,0));
          p=q;q++;r=0;
      }
      if(q==num_dot-1)ptag=-1;
   }
  
  return;
}
void line(HDC hdc,int p,int q,int width, COLORREF color)
{
  HPEN usepen;
  usepen=CreatePen(PS_SOLID,width,color);
  SelectObject(hdc,usepen); 
  MoveToEx(hdc,dot[p].x,dot[p].y,NULL);
  LineTo(hdc,dot[q].x,dot[q].y);
  SelectObject(hdc,GetStockObject(BLACK_PEN));  
  DeleteObject(usepen); 
  return;
} 
void cross(HDC hdc,int cx,int cy,int width,COLORREF color)
{
  HPEN usepen;
  usepen=CreatePen(PS_SOLID,width,color);
  SelectObject(hdc,usepen); 
  MoveToEx(hdc,cx-3,cy-3,NULL);
  LineTo(hdc,cx+3,cy+3);
  MoveToEx(hdc,cx-5,cy+3,NULL);
  LineTo(hdc,cx+3,cy-3);
  SelectObject(hdc,GetStockObject(BLACK_PEN));  
  DeleteObject(usepen); 
  return;	  
}
float xmul(int p,int q,int r)
{
  float ax,ay,bx,by;
  ax=dot[q].x-dot[p].x;  ay=dot[q].y-dot[p].y;
  bx=dot[r].x-dot[p].x;  by=dot[r].y-dot[p].y;
  return ax*by-bx*ay;
}
/* void disptheta(HDC hdc,float* theta,int cx,int cy,int n,COLORREF color)
{
  int i;
  char strbuf[80];
  SetTextColor(hdc,color);
  for(i=0;i<n;i++)
  {
    sprintf(strbuf,"i= %3d    cos = %7.4f   point at (%7.4f , %7.4f); \0",i,theta[i],dot[i].x,dot[i].y);
    TextOut(hdc,cx,cy+i*18,strbuf,strlen(strbuf));    
  }    
  return;
}
void dispinfo(HDC hdc, float info,int x,int y, COLORREF color)
{
   int cx,cy;
   char strbuf[80];
   cx=(x-400)/130;cy=(y-20)/20;
   sprintf(strbuf, "X(%d,%d):   %7.2f\0",cx,cy,info);
   SetTextColor(hdc,color);
   TextOut(hdc,x,y,strbuf,strlen(strbuf));
   return;
}
*/


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值