The brute force algorithm is used to solve the convex hull problem.

Source: Internet
Author: User

/*************************************** **************/
/**/
/* Solve the convex hull problem */
/* Use the brute force algorithm */
/* Author: lixiongwei */
/* Time: 06/12/11 sun .*/
/* Win XP + (TC/win_tc/VC + + 6.0 )*/
/**/
/*************************************** **************/
# Include <stdio. h>
# Include <conio. h>
# Include <time. h>
# Include <stdlib. h>
# Include <graphics. h>
# Define max_x 630
# Define max_y 470
# Define max_num 200
/***************** Function prototype and variable type declaration *************** ********/
Struct point {
Double X;/* x coordinate */
Double Y;/* Y coordinate */
Int flag;/* mark convex points */
};
Struct point my_point [max_num];/* defines the Convex Point Information */
Void my_graphics ();
Void my_convex_hull ();
/***************************** Main function ******** **********************/
Int main ()
{
Int I, J, K;
Clock_t start, end;
Double elapsed, temp_x, temp_y;

/* Initialize Random Number */
Srand (unsigned) Time (null ));
Start = clock ();
For (I = 0; I <max_num; I ++)
{
While (my_point [I]. x = (RAND () % max_x) <10);/* generates abscissa randomly */
While (my_point [I]. Y = (RAND () % max_y) <10);/* randomly generates Y coordinates */
My_point [I]. Flag = 0;
}
/**********************************/
For (I = 0; I <max_num; I ++)
Printf ("my_point [% d]. x = % F my_point [% d]. Y = % F my_point [% d]. Flag = % d/N ",
I, my_point [I]. X, I, my_point [I]. Y, I, my_point [I]. Flag );

Printf ("/Nnow compute, press any key to continue.../N ");
Getch ();
My_convex_hull ();/* call the convex hull calculation function */
For (I = 0; I <max_num; I ++)
Printf ("my_point [% d]. x = % F my_point [% d]. Y = % F my_point [% d]. Flag = % d/N ",
I, my_point [I]. X, I, my_point [I]. Y, I, my_point [I]. Flag );

Printf ("/npress any key to watch screen.../N ");
Getch ();
My_graphics ();
End = clock ();
Elapsed = (double) (end-Start)/clk_tck;
Printf ("this program use time: % fs. Goodbye everyone ~! /N ", elapsed );
Getch ();
Return 0;
}

/***************** Use the brute force algorithm to evaluate the definition of the convex hull computing function ************ *******/
Void my_convex_hull ()
{
Int I, J, K, Sign = 0;
Double A = 0, B = 0, c = 0;

For (I = 0; I <max_num; ++ I)
For (j = I + 1; j <max_num; ++ J)
{
A = my_point [J]. Y-my_point [I]. Y;
B = my_point [I]. X-my_point [J]. X;
C = (my_point [I]. x * my_point [J]. Y)-(my_point [I]. y * my_point [J]. X );

Sign = 0;
For (k = 0; k <max_num; ++ K)
{
If (k = J) | (k = I ))
Continue;
/*
If (A * my_point [K]. x + B * my_point [K]. y) = C)
Exit (1 );
* // * If more than two vertices are in the same line, do not process them */

If (A * (my_point [K]. X) + B * (my_point [K]. y)> C ))
++ Sign;
If (A * (my_point [K]. X) + B * (my_point [K]. y) <C ))
-- Sign;

}/* End inside */

If (Sign = (MAX_NUM-2) | (Sign = (2-max_num ))))
{
Printf ("/n ");
My_point [I]. Flag = 1;
My_point [J]. Flag = 1;
}

}/* Inside for end */

Printf ("/n ");
}

/****************** Function Definition of the screen output function *************** ****/
Void my_graphics ()
{
Int I, J, K, convex_hull, sign;
Double A = 0, B = 0, c = 0;
Struct point border_point [max_num];/* record boundary convex points */
Int graphdriver = VGA;
Int graphmode = vgahi;

Initgraph (& graphdriver, & graphmode ,"");
Cleardevice ();
Setcolor (white );
Rectangle (0, 0, 639,479 );
Rectangle (2, 2, 637,477 );

For (I = 0, j = 0; I <max_num; I ++)
{
If (my_point [I]. Flag = 1)
{
Border_point [J]. x = my_point [I]. X;
Border_point [J]. Y = my_point [I]. Y;
Border_point [J]. Flag = 1;
++ J;
}
}
Convex_hull = J;/* records the number of vertices */

For (I = 0; I <max_num; ++ I)
{
If (my_point [I]. Flag = 1)
{
Setcolor (yellow );
Fillellipse (my_point [I]. X, my_point [I]. Y, 3, 3 );
}
Else
{
Setcolor (white );
Circle (my_point [I]. X, my_point [I]. Y, 2 );
}
}

*************/
Setcolor (white );
For (I = 0; I <convex_hull; ++ I)
For (j = I + 1; j <convex_hull; ++ J)
{
A = border_point [J]. Y-border_point [I]. Y;
B = border_point [I]. X-border_point [J]. X;
C = (border_point [I]. x * border_point [J]. Y)-(border_point [I]. y * border_point [J]. X );

Sign = 0;
For (k = 0; k <convex_hull; ++ K)
{
If (k = J) | (k = I ))
Continue;
If (A * (border_point [K]. X) + B * (border_point [K]. y)> C ))
++ Sign;
If (A * (border_point [K]. X) + B * (border_point [K]. y) <C ))
-- Sign;

}/* End inside */

If (Sign = (convex_hull-2) | (Sign = (2-convex_hull ))))
{
Line (border_point [I]. X, border_point [I]. Y, border_point [J]. X, border_point [J]. y );

}

}/* Inside for end */
/**************************/

Getch ();
Closegraph ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.