E : Pentagram-shaped stars

05Dec12

I recently participated in the Advent Programming Contest organised by the IEEE Student Branch Klagenfurt (with support from Alpen-Adria-Universität Klagenfurt and Universität Passau), achieving a rank of 11th (out of 118 participants); as submissions of solutions to that contest are now closed, I’m posting here the solutions I submitted to that contest.

Problem (E; medium difficulty):

Pentagram-shaped stars

Santa tells the elves to produce some decorative stars cut out of paper. The elves use different sizes of regular pentagram shapes, which they cut according to the following layout:

Advent Programming Contest – E

The stars look nice, but Santa is concerned about the clippings which are wasted for each star.

Problem

Your task is to implement a program that calculates the cut-off area for a star of given size. Your program shall read the length x (see figure above) from standard input, calculate the cut-off area (red area in the figure above) and print the answer with two digits behind the decimal point. Then the programm shall terminate.

Example

Input

0.5

Output

0.16

Solution (E.c):

#include <stdio.h>
#include <math.h>

double f_Angle2Radian( const double p )
{
  return p * ( M_PI / 180 );
}

int main( void )
{
  double x;
  scanf( "%lf", &x );

  double tri_length = 1;
  double tri_base = ( 2 * pow( tri_length, 2 )) - ( 2 * tri_length * tri_length * cos( f_Angle2Radian( 36 )));
  tri_base = sqrt( tri_base );
  //double tri_height = ( tri_base / 2 ) / tan( f_Angle2Radian( 18 ));

  double l = tri_length + tri_base + tri_length;

  tri_length /= l;
  tri_base /= l;

  double x_tri_length = x * tri_length;
  double x_tri_base = x * tri_base;
  double x_tri_height = ( x_tri_base / 2 ) / tan( f_Angle2Radian( 18 ));
  double x_tri_area = ( x_tri_base / 2 ) * x_tri_height;
  x_tri_area /= 2;
  x_tri_area *= 10;

  double x_pent_base = x_tri_base;
  double x_pent_height = ( x_pent_base / 2 ) / tan( f_Angle2Radian( 36 ));
  double x_pent_area = ( x_pent_base / 2 ) * x_pent_height;
  x_pent_area /= 2;
  x_pent_area *= 10;

  double H = x_tri_length + x_tri_base + x_tri_length;
  double O2 = ( 2 * pow( H, 2 )) - ( 2 * H * H * cos( f_Angle2Radian( 36 )));
  O2 = sqrt( O2 );
  double O = O2 / 2;
  double A = O / tan( f_Angle2Radian( 18 ));

  double y = A;

  double area = x * y;
  area -= x_tri_area;
  area -= x_pent_area;

  printf( "%.2f\n", area );
  return 0;
}

Testing:

gcc -std=c99 -Wall -lm E.c
mv a.out E
./E
0.5
0.16
Advertisements


No Responses Yet to “E : Pentagram-shaped stars”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: