float dist_point_to_line( float* point, float* line1, float* line2 ) { float s, a, b, c, result; a = abs( distance_between_points( point, line1) ); b = abs( distance_between_points( line1, line2) ); c = abs( distance_between_points( point, line2) ); s = (a + b + c) / 2.0; result = (2 * sqrt(s * (s-a) * (s-b) * (s-c))) / c; return result; }
Pair_Point2D find_closest_pair_brute_force(std::vector<Point2D>& points) { Pair_Point2D closest_pair; float closest_distance = INFINITY; for(Point2D& p1 : points) { for(Point2D& p2 : points) { if(&p1 != &p2) { float tmp_distance = distance_between_points( p1, p2); if( tmp_distance < closest_distance) { closest_distance = tmp_distance; closest_pair = std::make_pair( p1, p2); } } } } return closest_pair; }
MatchingEnds matchingEndsForThreads(Thread_Hypoth* thread1, Thread_Hypoth* thread2, double distanceThreshold) { if (distance_between_points(thread1->start_pos(), thread2->start_pos()) < distanceThreshold) { return MatchingStartStart; } else if (distance_between_points(thread1->start_pos(), thread2->end_pos()) < distanceThreshold) { return MatchingStartEnd; } else if (distance_between_points(thread1->end_pos(), thread2->start_pos()) < distanceThreshold) { return MatchingEndStart; } else if (distance_between_points(thread1->end_pos(), thread2->end_pos()) < distanceThreshold) { return MatchingEndEnd; } return MatchingNone; }
void draw_line2(point p1,point p2) { if(light_transport_mode) { int i = 0; float dist = distance_between_points(p1,p2); float d0 = dist/8; float d1 = rand_num()*d0; float theta = atan2((p2.y-p1.y),(p2.x-p1.x)); float tot_dist = d1; float ctheta = cos(theta),stheta = sin(theta); p2.x = p1.x + d1*ctheta; p2.y = p1.y + d1*stheta; glBegin(GL_LINES); glVertex2f(p1.x,p1.y); glVertex2f(p2.x,p2.y); while(1) { p1 = p2; if(tot_dist+d0 > dist) { d0 = dist - tot_dist; p2.x = p1.x + d0*ctheta; p2.y = p1.y + d0*stheta; if(i%2) { glVertex2f(p1.x,p1.y); glVertex2f(p2.x,p2.y); } break; } p2.x = p1.x + d0*ctheta; p2.y = p1.y + d0*stheta; if(i%2) { glVertex2f(p1.x,p1.y); glVertex2f(p2.x,p2.y); } tot_dist += d0; i++; } glEnd(); return; } glBegin(GL_LINES); glVertex2f(p1.x,p1.y); glVertex2f(p2.x,p2.y); glEnd(); }
void gaze_cursor(int value) { if(!gaze_cursor_mode) { glutTimerFunc(10,gaze_cursor,0); return; } int i = 0; for(i=0; i<num_projectors; i++) { point center; center.x = (projector[i].l.p1.x + projector[i].l.p2.x)/2; center.y = (projector[i].l.p1.y + projector[i].l.p2.y)/2; float m = tan(atan2(center.y - mouse.p.y , center.x - mouse.p.x)); float m1 = -1.0/m; float x3 = center.x; float y3 = center.y; float l = distance_between_points(projector[i].l.p1,projector[i].l.p2); float val = sqrt((l*l)/(4+4*m1*m1)); float c1 = y3 - m1*x3; float x2 = x3 + val; float x1 = 2*x3 - x2; float y1 = x1*m1 + c1; float y2 = x2*m1 + c1; projector[i].l.p1.x = x1; projector[i].l.p1.y = y1; projector[i].l.p2.x = x2; projector[i].l.p2.y = y2; if(find_side(projector[i].l.p1,projector[i].l.p2,projector[i].d) == -1) { float x2 = x3 - val; float x1 = 2*x3 - x2; float y1 = x1*m1 + c1; float y2 = x2*m1 + c1; projector[i].l.p1.x = x1; projector[i].l.p1.y = y1; projector[i].l.p2.x = x2; projector[i].l.p2.y = y2; } projector[i].l.m = m1; projector[i].l.c = c1; //projector[i].l = find_slope_intercept(projector[i].l); projector[i].d = find_source_point(projector[i]); divide_line(projector[i].l.p1,projector[i].l.p2,projector[i].num_pixels,i); } glutTimerFunc(10,gaze_cursor,0); }
int motionnotify_ev(int x, int y) { float coeff; if (env->block_events) return (0); if (env->pressed_mouse) { if (env->selected_object) { coeff = distance_between_points(&env->selected_object->origin, &env->scene->camera.origin) * 0.001; if (env->pressed_keys.alt) { env->selected_object->radius += fmin((float)(x - env->mouse_x), (float)(y - env->mouse_y)) * coeff; if (env->selected_object->radius < 0) env->selected_object->radius *= -1; } else if (env->pressed_keys.tab) { normalize_vector(&env->selected_object->normal); vect_rot_x(&env->selected_object->normal, -cos(x - env->mouse_x) / (5000 * coeff)); } else { vector_add((t_vector*)&(env->selected_object->origin), &env->scene->camera.y_axis, (env->mouse_x - x) * coeff); if (env->pressed_keys.ctrl) vector_add((t_vector*)&(env->selected_object->origin), &env->scene->camera.x_axis, (env->mouse_y - y) * coeff); else vector_add((t_vector*)&(env->selected_object->origin), &env->scene->camera.z_axis, (env->mouse_y - y) * coeff); } } env->last_scene_change = clock(); } env->mouse_x = x; env->mouse_y = y; return (0); }
int main( int argc, char *argv[] ) { STRING src_polygons_filename; File_formats format; int n_src_objects, poly; object_struct **src_objects; polygons_struct *polygons, unit_sphere; Real x, y, z, xs, ys, zs, u, v, dist; Point point, centre, unit_point, poly_point; initialize_argument_processing( argc, argv ); if( !get_string_argument( NULL, &src_polygons_filename ) || !get_real_argument( 0.0, &x ) || !get_real_argument( 0.0, &y ) || !get_real_argument( 0.0, &z ) ) { print_error( "Usage: %s input.obj x y z\n", argv[0] ); return( 1 ); } if( input_graphics_file( src_polygons_filename, &format, &n_src_objects, &src_objects ) != OK ) return( 1 ); if( n_src_objects != 1 || get_object_type( src_objects[0] ) != POLYGONS || !is_this_tetrahedral_topology( get_polygons_ptr(src_objects[0]) ) ) { print( "First argument must contain one tetrahedral mesh.\n" ); return( 1 ); } polygons = get_polygons_ptr(src_objects[0]); create_polygons_bintree( polygons, ROUND( (Real) polygons->n_items * BINTREE_FACTOR ) ); fill_Point( centre, 0.0, 0.0, 0.0 ); create_tetrahedral_sphere( ¢re, 1.0, 1.0, 1.0, polygons->n_items, &unit_sphere ); fill_Point( point, x, y, z ); poly = find_closest_polygon_point( &point, polygons, &poly_point ); dist = distance_between_points( &point, &poly_point ); if( dist > 1.0 ) print( "Warning point is %.1f millimetres from the surface.\n", dist ); map_point_to_unit_sphere( polygons, &point, &unit_sphere, &unit_point ); xs = RPoint_x( unit_point ); ys = RPoint_y( unit_point ); zs = RPoint_z( unit_point ); map_sphere_to_uv( xs, ys, zs, &u, &v ); print( "%g %g %g -> %g %g\n", x, y, z, u, v ); delete_object_list( n_src_objects, src_objects ); delete_polygons( &unit_sphere ); return( 0 ); }
void rotate_object(float rot_val,int side,int selected,int i) { if(selected == PROJECTOR) { float x2,y2,c,m,d; m = tan(atan(projector[i].l.m) + rot_val); c = projector[i].l.p1.y - m*projector[i].l.p1.x; d = distance_between_points(projector[i].l.p1,projector[i].l.p2); x2 = projector[i].l.p1.x + sqrt( (d*d) / (1 + m*m)); y2 = m*x2 + c; point p; p.x=x2,p.y=y2; if(find_side(projector[i].l.p1,projector[i].l.p2,p) == side) { x2 = projector[i].l.p1.x - sqrt( (d*d) / (1 + m*m)); y2 = m*x2 + c; } if( abs(x2) >= world.width/2 || abs(y2) >= world.height/2 ) return; projector[i].l.m = m; projector[i].l.c = c; projector[i].l.p2.x = x2; projector[i].l.p2.y = y2; projector[i].l = find_slope_intercept(projector[i].l); projector[i].d = find_source_point(projector[i]); divide_line(projector[i].l.p1,projector[i].l.p2,projector[i].num_pixels,i); } else if(selected == BLOCK) { float x2,y2,c,m,d; m = tan(atan(block[i].l.m) + rot_val); c = block[i].l.p1.y - m*block[i].l.p1.x; d = distance_between_points(block[i].l.p1,block[i].l.p2); x2 = block[i].l.p1.x + sqrt( (d*d) / (1 + m*m)); y2 = m*x2 + c; point p; p.x=x2,p.y=y2; if(find_side(block[i].l.p1,block[i].l.p2,p) == side) { x2 = block[i].l.p1.x - sqrt( (d*d) / (1 + m*m)); y2 = m*x2 + c; } if( abs(x2) >= world.width/2 || abs(y2) >= world.height/2 ) return; block[i].l.m = m; block[i].l.c = c; block[i].l.p2.x = x2; block[i].l.p2.y = y2; block[i].l = find_slope_intercept(block[i].l); } else { float x2,y2,c,m,d; m = tan(atan(mirror[i].l.m) + rot_val); c = mirror[i].l.p1.y - m*mirror[i].l.p1.x; d = distance_between_points(mirror[i].l.p1,mirror[i].l.p2); x2 = mirror[i].l.p1.x + sqrt( (d*d) / (1 + m*m)); y2 = m*x2 + c; point p; p.x=x2,p.y=y2; if(find_side(mirror[i].l.p1,mirror[i].l.p2,p) == side) { x2 = mirror[i].l.p1.x - sqrt( (d*d) / (1 + m*m)); y2 = m*x2 + c; } if( abs(x2) >= world.width/2 || abs(y2) >= world.height/2 ) return; mirror[i].l.m = m; mirror[i].l.c = c; mirror[i].l.p2.x = x2; mirror[i].l.p2.y = y2; mirror[i].l = find_slope_intercept(mirror[i].l); } }
void draw_world() { world_z = world.height+25; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); glTranslatef(0.0f, 0.0f, -world_z); int i,j,k; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); int c = 0; draw_box(world.width,world.height); glColor3f(color[c].r,color[c].g,color[c].b); for(i=0; i<num_projectors; i++) { if(select_type == PROJECTOR && select_number == i) draw_selected_line(projector[i].l); else draw_line(projector[i].l); draw_point(projector[i].d); glColor3f(color[c].r,color[c].g,color[c].b); } c++; glColor3f(color[c].r,color[c].g,color[c].b); for(i=0; i<num_mirrors; i++) { if(select_type == MIRROR && select_number == i) draw_selected_line(mirror[i].l); else draw_line(mirror[i].l); glColor3f(color[c].r,color[c].g,color[c].b); } c++; glColor3f(color[c].r,color[c].g,color[c].b); for(i=0; i<num_blocks; i++) { if(select_type == BLOCK && select_number == i) draw_selected_line(block[i].l); else draw_line(block[i].l); glColor3f(color[c].r,color[c].g,color[c].b); } /*for(i=0; i<num_projectors; i++) { for(j=0; j<projector[i].num_pixels; j++) draw_line(projector[i].pixels[j]); draw_point(projector[i].d); }*/ for(i=0; i<num_projectors; i++) { for(j=0; j<projector[i].num_pixels; j++) { glColor3f(color[c].r,color[c].g,color[c++].b); point p,fp; float dist = INF; int type = 1,num; for(k=0; k<num_mirrors; k++) { p = find_intersection(mirror[k].l,projector[i].pixels[j]); if( (abs(p.x) <= world.width/2) && (abs(p.y) <= world.height/2) && check_point_on_line_segment(mirror[k].l,p) ) if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1) { float d = distance_between_points(p,projector[i].pixels[j].p1); if( d < dist ) { fp = p; dist = d; type = 1; num = k; if(find_side(mirror[k].l.p1,mirror[k].l.p2,projector[i].pixels[j].p1) == -1) type = 2; } } } for(k=0; k<num_blocks; k++) { p = find_intersection(block[k].l,projector[i].pixels[j]); if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(block[k].l,p)) if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1) { float d = distance_between_points(p,projector[i].pixels[j].p1); if( d < dist ) { fp = p; dist = d; type = 2; } } } for(k=0; k<num_world; k++) { p = find_intersection(world.l[k],projector[i].pixels[j]); if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(world.l[k],p)) if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1) { float d = distance_between_points(p,projector[i].pixels[j].p1); if( d < dist ) { fp = p; dist = d; type = 2; } } } for(k=0; k<num_projectors; k++) { if(k!=i) { p = find_intersection(projector[k].l,projector[i].pixels[j]); if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(projector[k].l,p)) if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1) { float d = distance_between_points(p,projector[i].pixels[j].p1); if( d < dist ) { fp = p; dist = d; type = 2; } } } } draw_line2(fp,projector[i].pixels[j].p2); if(type==1) { //float angle = find_angle(projector[i].pixels[j],mirror[num].l); //float angle = find_angle2(projector[i].pixels[j].p1,fp,mirror[num].l.p1,mirror[num].l.p2); line n; n.p1 = fp; n.m = tan(PI + 2*atan(mirror[num].l.m) - atan(projector[i].pixels[j].m)); n.c = n.p1.y - n.p1.x*n.m; draw_reflections(n,num,1); } } } //glMatrixMode(GL_MODELVIEW); //glLoadIdentity(); //glPushMatrix(); //glTranslatef(0.0f, 0.0f, -5.0f); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //glRotatef(rot,1,2,0); //glRasterPos2f(-0.5,0); //glColor4f(0.0f, 1.0f, 1.0f,1.0f); //glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, "Rishi Raj Singh Jhelumi"); //glBegin(GL_POINTS); //glVertex3f(0,0,-world_z); //glEnd(); //glPopMatrix(); glFlush(); //glutSwapBuffers(); }
void draw_reflections(line l,int m,int side) { int i,j,k; point p,fp; float dist = INF; int type = 1,num; for(k=0; k<num_mirrors; k++) { if(k!=m) { p = find_intersection(mirror[k].l,l); if( (abs(p.x) <= world.width/2) && (abs(p.y) <= world.height/2) && check_point_on_line_segment(mirror[k].l,p) ) if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side) { float d = distance_between_points(p,l.p1); if( d < dist ) { fp = p; dist = d; type = 1; num = k; if(find_side(mirror[k].l.p1,mirror[k].l.p2,l.p1) == -1) type = 2; } } } } for(k=0; k<num_blocks; k++) { p = find_intersection(block[k].l,l); if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(block[k].l,p)) if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side) { float d = distance_between_points(p,l.p1); if( d < dist ) { fp = p; dist = d; type = 2; } } } for(k=0; k<num_world; k++) { p = find_intersection(world.l[k],l); if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(world.l[k],p)) if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side) { float d = distance_between_points(p,l.p1); if( d < dist ) { fp = p; dist = d; type = 2; } } } for(k=0; k<num_projectors; k++) { p = find_intersection(projector[k].l,l); if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(projector[k].l,p)) if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side) { float d = distance_between_points(p,l.p1); if( d < dist ) { fp = p; dist = d; type = 2; } } } draw_line2(fp,l.p1); if(type==1) { //float angle = find_angle(l,mirror[m].l); line n; n.p1 = fp; n.m = tan(PI + 2*atan(mirror[num].l.m) - atan(l.m)); n.c = n.p1.y - n.p1.x*n.m; draw_reflections(n,num,1); } }
int check_point_on_line_segment(line l,point p) { float x = abs(distance_between_points(l.p1,p) + distance_between_points(p,l.p2) - distance_between_points(l.p1,l.p2)); return x < EPS; }
void set_3_4_view( Camera* camera, float position[3], float forward[3], float up[3], int delta ) { float ideal_position[3], ideal_up[3], // The ideal values position_distance, up_distance, // The distance from ideals position_correction, up_correction, // The speed at which to correct position_movement, up_movement; // The amount of correction // Set the camera's forward vector camera->forward[0] = position[0]; camera->forward[1] = position[1]; camera->forward[2] = position[2]; // Find the camera's ideal position ideal_position[0] = position[0] + (-0.05 * fabs(forward[0]) + 1.5 * up[0]) * camera_distance; ideal_position[1] = position[1] + (-0.05 * fabs(forward[1]) + 1.5 * up[1]) * camera_distance; ideal_position[2] = position[2] + (-0.05 * fabs(forward[2]) + 1.5 * up[2]) * camera_distance; // Find the distance between the camera's current position and its ideal // position position_distance = distance_between_points( ideal_position, camera->position ); up_distance = distance_between_points( ideal_up, camera->up ); // As the distance between what it is and what it should be increases, so // too does the amount we move it to catch up. position_correction = position_distance * position_distance; up_correction = up_distance * up_distance; // Calculate the movement position_movement = position_correction * (delta / 1000.0f) + CAMERA_TRACK_STRENGH; up_movement = up_correction * (delta / 1000.0f) + CAMERA_TRACK_STRENGH; // Correct the camera position /*if( position_movement > position_distance ) {*/ camera->position[0] = ideal_position[0]; camera->position[1] = ideal_position[1]; camera->position[2] = ideal_position[2]; /*}*/ /*else if( position_movement != 0 ) { camera->position[0] += position_movement * (ideal_position[0] - camera->position[0]); camera->position[1] += position_movement * (ideal_position[1] - camera->position[1]); camera->position[2] += position_movement * (ideal_position[2] - camera->position[2]); }*/ // Correct the camera up vector /*if( up_movement != 0 ) { camera->up[0] += up_movement * (ideal_up[0] - camera->up[0]); camera->up[1] += up_movement * (ideal_up[1] - camera->up[1]); camera->up[2] += up_movement * (ideal_up[2] - camera->up[2]); }*/ camera->up[0] = 0;//ideal_up[0]; camera->up[1] = 1;//ideal_up[1]; camera->up[2] = 0;//ideal_up[2]; // Ensure that camera is above terrain Landscape* landscape = get_gamestate()->landscape; float terrain_height = Landscape_getHeight( landscape, camera->position[0], camera->position[2] ); if( camera->position[1] < terrain_height + min_camera_height ) { camera->position[1] = terrain_height + min_camera_height; } }
Pair_Point2D find_closest_pair_divide_and_conquer(std::vector<Point2D>& points) { if(points.size() > 3) { //splitting the vector int extra = 0; if(points.size()%2 != 0) { extra = 1; } int it = std::floor(points.size()/2.0); std::vector<Point2D> left{ points.begin(), (points.end()-it)}; std::vector<Point2D> right{ (points.begin()+it)+extra, points.end()}; //std::cout << points.size() << " | " << left.size() << " | " << right.size() << std::endl; //finding the pairs recursively Pair_Point2D p1 = find_closest_pair_divide_and_conquer(left); Pair_Point2D p2 = find_closest_pair_divide_and_conquer(right); float d1, d2; d1 = distance_between_points(p1.first, p1.second); d2 = distance_between_points(p2.first, p2.second); //checking the strip float d = std::min( d1, d2); std::vector<Point2D> strip; for(Point2D p : points) { if(std::abs(p._x - points[it]._x) < d) { strip.push_back(p); } } Pair_Point2D p_strip = find_closest_pair_brute_force(strip); if(distance_between_points(p_strip.first, p_strip.second) < d) { return p_strip; } if(d1 < d2) { return p1; } else { return p2; } } else { return find_closest_pair_brute_force(points); } }
int main(int argc, char const *argv[]) { for (int i = 0; i < COUNT; ++i) { std::cout << "Test: " << i << " of " << COUNT << std::endl; std::vector<Point2D> points, points_sorted_x, points_sorted_y; //initializing random int generator. Values from 1 to SIZE std::mt19937 rng; rng.seed(std::random_device()()); std::uniform_int_distribution<std::mt19937::result_type> distance( 1, SIZE); for (int i = 0; i < POINT_COUNT; ++i) { points.push_back(Point2D(distance(rng), distance(rng))); } //print(points); /* finding the closest pair using brute force */ Pair_Point2D tmp_brute = find_closest_pair_brute_force(points); std::cout << "Finding closest pair (Brute Force)" << std::endl << tmp_brute.first << " and " << tmp_brute.second << " Distance: " << distance_between_points(tmp_brute.first, tmp_brute.second) << std::endl; /* finding the closest pair using divide and conquer */ //print(points); //sorting for x std::sort(points.begin(), points.end(), [](Point2D p1, Point2D p2) { return p1._x < p2._x; }); points_sorted_x = points; /* //sorting for y std::sort(points.begin(), points.end(), [](Point2D p1, Point2D p2) { return p1._y < p2._y; }); points_sorted_y = points; */ Pair_Point2D tmp_divide = find_closest_pair_divide_and_conquer(points_sorted_x); std::cout << "Finding closest pair (Divide and Conquer)" << std::endl << tmp_divide.first << " and " << tmp_divide.second << " Distance: " << distance_between_points(tmp_divide.first, tmp_divide.second) << std::endl; std::cout << "--------------------------" << std::endl; } return 0; }