Пример #1
0
int main()
try {
    int width, height;
    cin >> width >> height;
    print_area(width, height);
}
catch (runtime_error e) {
    cout << "caught in main: " << e.what() << "\n";
}
Пример #2
0
// Function to perform the monte carlo integration
void monte_carlo(frame &anim, double threshold, double box_length){

    // Creating random numbers with mt19937
    static std::random_device rd;
    int seed = rd();
    static std::mt19937 gen(seed);

    // integer to hold the max number our vectors should count to
    int vec_count = 1, prev_print_count = 0;

    // Creating vector to hold points for visualization later
    std::vector<pos> points(1024);
    std::vector<color> pt_clrs(1024), area_clrs(1024), pe_clrs(1024);

    color pt_clr, area_clr, pe_clr;

    double count_in = 0; 
    double ratio;

    std::vector<double> area(1024), pe_vec(1024);
    //double true_area = circle_area(0.5 * box_length);
    double true_area = batman_area(anim.res_x * 0.03);
    double temp_area;
    //double container_area = box_length * box_length;

    pos oval_radius;
    oval_radius.x = box_length / 2.0;
    oval_radius.y = box_length / 4.0;
    double container_area = oval_area(oval_radius);

    // distribution for box
    std::uniform_real_distribution<double> box_dist(-0.5,0.5);

    // distribution for oval?
    //std::uniform_real_distribution<double> oval_dist;

    // defining location of dot
    pos loc;

    // pe is the percent error -- setting arbitrarily high for now...
    double pe = 10;

    std::cout << "performing monte_carlo..." << '\n';

    // number of generations to wor with
    int iterations = 200;
    int final_count = 1;

    for (int i = 0; i < iterations; ++i){
        if (final_count < 1024){
            final_count *= 2;
        }
        else{
            final_count += 1024;
        }
    }

    //while (abs(pe) > threshold){
    for (int count = 1; count < final_count; ++count){

        loc.x = box_dist(gen) * box_length + anim.origin.x;
        loc.y = box_dist(gen) * box_length + anim.origin.y;
        //while (!in_square(loc, box_length, anim.origin)){
        while(!in_oval(loc, oval_radius, anim.origin)){
            //std::cout << "regenerating point" << '\n';
            loc.x = box_dist(gen) * box_length + anim.origin.x;
            loc.y = box_dist(gen) * box_length + anim.origin.y;
        }

        if (is_batman(loc, anim.origin, anim.res_x * 0.03)){
            count_in += 1;
            pt_clr.b = 0.0;
            pt_clr.g = 0.0;
            pt_clr.r = 0.0;
        }
        else{
            pt_clr.b = 0.0;
            pt_clr.g = 1.0;
            pt_clr.r = 1.0;
        }
/*
        
        // Color scheme for circle
        if (in_circle(anim, loc, box_length/2)){
            count_in += 1;
            pt_clr.b = 0.0;
            pt_clr.g = 1.0;
            pt_clr.r = 0.0;
        }
        else{
            pt_clr.b = 0.0;
            pt_clr.g = 0.0;
            pt_clr.r = 1.0;
        }
*/

        //points.push_back(loc);
        points[count - prev_print_count - 1] = loc;
        //std::cout << count - prev_print_count << '\t' 
        //          << points[count - prev_print_count - 1].x << '\t' 
        //          << points[count - prev_print_count - 1].y << '\n';
 
        //pt_clrs.push_back(pt_clr);
        pt_clrs[count - prev_print_count - 1] = pt_clr;

        temp_area = (((double)count_in/(double)count)*container_area);
        ratio = ((double)count_in/(double)count);
        //area.push_back(temp_area);

        pe = (temp_area - true_area) / true_area;
        //pe_vec.push_back(abs(pe));

        if (abs(pe) < threshold){
            area_clr.r = 0;
            area_clr.g = 1;
            area_clr.b = 0;
            pe_clr.r = 0;
            pe_clr.g = 1;
            pe_clr.b = 0;
        }
        if (abs(pe) >= threshold){
            area_clr.r = 1;
            area_clr.g = 0;
            area_clr.b = 0;
            pe_clr.r = 1;
            pe_clr.g = 0;
            pe_clr.b = 0;

        }

        //area_clrs.push_back(area_clr);
        //pe_clrs.push_back(pe_clr);

/*
        draw_point(anim, loc, pt_clr);
        if (anim.curr_frame + 1 < num_frames){
            anim.curr_frame++;
        }
*/

        if (count - prev_print_count == vec_count){
            //std::cout << "printing..." << '\n';
            for (int j = 0; j < vec_count; ++j){
                draw_point(anim, points[j], pt_clrs[j]);
                //std::cout << points[j].x << '\t' << points[j].y << '\n';
            }
            print_area(anim, ratio, area_clr);
            print_pe(anim, abs(pe), pe_clr);
            print_count(anim, count);
            if (vec_count < 1024){
                vec_count *= 2;
            }
            if (anim.curr_frame + 1 < num_frames){
                anim.curr_frame++;
            }
            prev_print_count = count;
        }

        //std::cout << count << '\t' << vec_count << '\t' 
        //          << prev_print_count << '\t' << temp_area 
        //          << '\t' << pe << '\n';

        //std::cout << count << '\n';

    }

    //std::cout << anim.curr_frame << '\n';

}
Пример #3
0
/*
 * Name: dissect_osi_options()
 *
 * Description:
 *   Main entry area for esis de-mangling.  This will build the
 *   main esis tree data and call the sub-protocols as needed.
 *
 * Input:
 *   guchar       : length of option section
 *   tvbuff_t *   : tvbuff containing packet data
 *   int          : offset into packet where we are (packet_data[offset]== start
 *                  of what we care about)
 *   proto_tree * : tree of display data.  May be NULL.
 *
 * Output:
 *   void, but we will add to the proto_tree if it is not NULL.
 */
void
dissect_osi_options( guchar opt_len, tvbuff_t *tvb,
                     int offset, proto_tree *tree) {
   proto_item *ti;
   proto_tree *osi_option_tree = NULL;
   guchar      parm_len        = 0;
   guchar      parm_type       = 0;
   guint8      octet;

   if (tree) {
     if ( 0 == opt_len ) {
       proto_tree_add_text( tree, tvb, offset, 0,
                            "### No Options for this PDU ###" );
       return;
     }

     ti = proto_tree_add_text( tree, tvb, offset, opt_len,
                               "### Option Section ###" );
     osi_option_tree = proto_item_add_subtree( ti, ott_osi_options );

     while ( 0 < opt_len ) {
        parm_type   = (int) tvb_get_guint8(tvb, offset);
        offset++;
        parm_len    = (int) tvb_get_guint8(tvb, offset);
        offset++;

        switch ( parm_type ) {
          case   OSI_OPT_QOS_MAINTANANCE:
                 octet = tvb_get_guint8(tvb, offset);
                 dissect_option_qos( (guchar) (octet&OSI_OPT_QOS_MASK),
                                     (guchar) (octet&OSI_OPT_QOS_SUB_MASK),
                                     offset, parm_len, tvb, osi_option_tree );
          break;
          case   OSI_OPT_SECURITY:
                 octet = tvb_get_guint8(tvb, offset);
								 if ( clnp_decode_atn_options ){
									 dissect_option_atn_security_label(octet,parm_len,tvb, offset, osi_option_tree );
								 }else {
									proto_tree_add_text( osi_option_tree, tvb, offset, parm_len,
                  "Security type: %s",
                  val_to_str( octet&OSI_OPT_SEC_MASK,
                              osi_opt_sec_vals, "Unknown (0x%x)")  );	
								 }
																							

          break;
          case   OSI_OPT_PRIORITY:
                 octet = tvb_get_guint8(tvb, offset);
                 if ( OSI_OPT_MAX_PRIORITY >= octet ) {
                   proto_tree_add_text( osi_option_tree, tvb, offset, parm_len,
                                        "Priority    : %u", octet );
                 }
                 else {
                   proto_tree_add_text( osi_option_tree, tvb, offset, parm_len,
                                        "Priority    : %u ( Invalid )",
                                        octet );
                 }
          break;
          case   OSI_OPT_ADDRESS_MASK:
                 proto_tree_add_text( osi_option_tree, tvb, offset, parm_len,
                  "Address Mask: %s",
		  print_area( tvb_get_ptr(tvb, offset, parm_len), parm_len ) );
          break;
          case   OSI_OPT_SNPA_MASK:
                 proto_tree_add_text( osi_option_tree, tvb, offset, parm_len,
                  "SNPA Mask   : %s",
		  print_system_id( tvb_get_ptr(tvb, offset, parm_len), parm_len ));
          break;
          case   OSI_OPT_ES_CONFIG_TIMER:
                 proto_tree_add_text( osi_option_tree, tvb, offset, parm_len,
                  "ESCT     : %u seconds", tvb_get_ntohs( tvb, offset ) );
          break;
          case   OSI_OPT_PADDING:
                 proto_tree_add_text( osi_option_tree, tvb, offset, parm_len,
                  "Padding  : %u Octets", parm_len ) ;
          break;
          case   OSI_OPT_SOURCE_ROUTING:
          case   OSI_OPT_RECORD_OF_ROUTE:
                 dissect_option_route( parm_type,
                                       offset, parm_len, tvb, osi_option_tree );
          break;
          case   OSI_OPT_REASON_OF_DISCARD:
                 dissect_option_rfd( tvb_get_guint8(tvb, offset),
                                     tvb_get_guint8(tvb, offset + 1),
                                     offset, parm_len, tvb, osi_option_tree );
          break;
        }
        opt_len -= parm_len + 2;
        offset  += parm_len;
      }
   }
} /* dissect-osi-options */