void normalize() { set<segment> T; set<point> U; for (int i = 0; i < n; i++) make_barrier(i, i); for (int i = 0; i < m; i++) { point p = v[b[i].first], q = v[b[i].second]; set<point> S; S.insert(p); S.insert(q); for (int j = 0; j < m; j++) { point r = v[b[j].first], s = v[b[j].second]; if (r == p || r == q || s == p || s == q) continue; if (cmp((q - p) % (s - r)) == 0) { if (between(p, r, q)) S.insert(r); if (between(p, s, q)) S.insert(s); } else if (seg_intersect(p, q, r, s)) { S.insert(line_intersect(p, q, r, s)); } } foreach (st, all(S)) { if (st != S.begin()) T.insert(segment(p, *st)); U.insert(p = *st); } } clear(); foreach (it, all(U)) v[n++] = *it; foreach (it, all(T)) { int i = lower_bound(v, v+n, it->first) - v; int j = lower_bound(v, v+n, it->second) - v; make_barrier(i, j); }
//intersection between two line segments //Returns true if the two segments intersect, //in which case intersection is set to the point of intersection bool line_segment_intersect(Point p1, Point p2, Point p3, Point p4, Point& intersection){ bool parallel; pair<double,double> u = line_intersect(p1,p2,p3,p4,parallel); if (parallel || u.first < 0 || u.first > 1 || u.second < 0 || u.second > 1) return false; intersection.x = p1.x + u.first*(p2.x - p1.x); intersection.y = p1.y + u.first*(p2.y - p1.y); return true; }
/* raySegIntersect: * Find the point p where ray v->w intersects segment ai-bi, if any. * Return 1 on success, 0 on failure */ static int raySegIntersect(pointf v, pointf w, pointf a, pointf b, pointf * p) { if (raySeg(v, w, a, b)) return line_intersect(v, w, a, b, p); else return 0; }
/******************************************************** * Longer Operations ********************************************************/ int Discrete_upper_envelope::bin_search_intersection( const Line& ll, const Line& hh, int low, int high){ // get the easy degerate case out of the way if (line_intersect(ll,hh,low)) { return low; } if (line_intersect(ll,hh,high)) { return high; } // label lines such that l is the lower line on the left const Line* l; const Line* h; if (line_below(ll,hh,low)) { l = ≪ h = &hh; } else { l = &hh; h = ≪ } // check if the lines intersect in the interval if ( line_below(*l, *h, high) ) { return -1; } // if we got here we know that the lines intersect in (low, high) // loop with invar the intersection is in [lb, hb) int ub = high, lb = low, m; while (ub-lb > 1) { rAssert( !line_above(*l,*h,lb) && line_above(*l, *h, ub)); // set new m m = lb + (ub-lb)/2; // check if the intervals swapped if ( !line_above(*l, *h, m)) { lb = m; } else { ub = m; } } return lb; }
static PyObject *web_line_intersect(PyObject *self, PyObject *args) { double x1,y1,x2,y2,r2; /* Parse the input tuple */ if (!PyArg_ParseTuple(args, "ddddd", &x1,&y1,&x2,&y2,&r2)) return NULL; /* Call the external C function to compute the area. */ double *intersect = line_intersect(0,0,x1,y1,x2,y2,r2); /* Build the output tuple */ PyObject *ret = Py_BuildValue("[d,d,d,d]",intersect[0],intersect[1],intersect[2],intersect[3]); return ret; }
double * /* distance between l1, l2 */ line_distance(LINE *l1, LINE *l2) { double *result; Point *tmp; result = PALLOCTYPE(double); if (line_intersect(l1, l2)) { *result = 0.0; return(result); } if (line_vertical(l1)) *result = fabs(l1->C - l2->C); else { tmp = point_construct(0.0, l1->C); result = dist_pl(tmp, l2); PFREE(tmp); } return(result); }
void Polygon::add(Point p) { int np = number_of_points(); if (1<np) { // check that thenew line isn't parallel to the previous one if (p==point(np-1)) error("polygon point equal to previous point"); bool parallel; line_intersect(point(np-1),p,point(np-2),point(np-1),parallel); if (parallel) error("two polygon points lie in a straight line"); } for (int i = 1; i<np-1; ++i) { // check that new segment doesn't interset and old point Point ignore(0,0); if (line_segment_intersect(point(np-1),p,point(i-1),point(i),ignore)) error("intersect in polygon"); } Closed_polyline::add(p); }
void Discrete_upper_envelope::build_brute_force( const std::vector< Line >& input, int LB, int UB) { typedef std::vector< Line >::const_iterator Line_iter; typedef std::list< Line_iter >::iterator Line_handel_iter; // init and verify init_build_and_verify_input(input, LB, UB); // the current set of upper lines and envelope cells being built Line_iter upper_line; for (int x = one() ; x <= U() ; ++x) { // set the first line to be the upper line upper_line = input.begin(); // iterate over lines find the lines that evaluates to the max val for (Line_iter li = DDAD_util::next(input.begin()) ; li != input.end() ; ++li) { if (line_above(*li, *upper_line, x) || (line_intersect(*li, *upper_line,x) && Line_2::slope_order(*li, *upper_line) == Line_2::SLOPE_ORDER_LESS)) { upper_line = li; } } // output the upper line rLog(up_env_brute, "Upper line %s", thing_to_cstring(*upper_line)); // setup new cell or expand last cell if (envelope_.size() == 0 || envelope_.back().line != *upper_line) { envelope_.push_back(Envelope_cell(*upper_line, x,x)); } else { envelope_.back().right = x; } } }
void drizzle_helper_functions::poly_edge_clip(std::vector<LocationType> sub, LocationType x0, LocationType x1, int left, std::vector<LocationType>* res) { int i, side0, side1; LocationType tmp; LocationType v0 = sub[sub.size()-1], v1; res->clear(); side0 = left_of(x0, x1, v0); if (side0 != -left) res->push_back(v0); for (i = 0; i < sub.size(); i++) { v1 = sub[i]; side1 = left_of(x0, x1, v1); if (side0 + side1 == 0 && side0) // last point and current point span the edge if (line_intersect(x0, x1, v0, v1, &tmp)) res->push_back(tmp); if (i == sub.size()-1) break; if (side1 != -left) res->push_back(v1); v0 = v1; side0 = side1; } }
void determine_hilight() { float min_dist = 1024; hl_wrect = NULL; hl_fpoly = NULL; hl_thing = NULL; // List of sectors the camera view ray passes through vector<int> sectors; // Check Things if (render_things > 0) { for (int t = 0; t < map.n_things; t++) { int f_height = 0; int height = things_3d[t]->sprite->height; if (things_3d[t]->parent_sector == -1) continue; if (sector_info[things_3d[t]->parent_sector].visible) { if (map.things[t]->ttype->hanging) f_height = map.sectors[things_3d[t]->parent_sector]->c_height - height; else f_height = map.sectors[things_3d[t]->parent_sector]->f_height; } if (map.things[t]->z != 0 && map.hexen) f_height += map.things[t]->z; int r = things_3d[t]->sprite->width / 2; if (map.things[t]->ttype->radius == -1) { r = 4; height = 8; f_height -= 4; } float x1 = (map.things[t]->x - camera.strafe.x * r) * SCALE_3D; float y1 = (map.things[t]->y - camera.strafe.y * r) * SCALE_3D; float x2 = (map.things[t]->x + camera.strafe.x * r) * SCALE_3D; float y2 = (map.things[t]->y + camera.strafe.y * r) * SCALE_3D; float dist = line_intersect(camera.position, camera.view, x1, y1, x2, y2); if (dist != -1 && dist < min_dist) { point3_t direction = camera.view - camera.position; point3_t hit_point(camera.position.x + (direction.x * dist), camera.position.y + (direction.y * dist), camera.position.z + (direction.z * dist)); if (hit_point.z >= f_height * SCALE_3D && hit_point.z <= (f_height + height) * SCALE_3D) { min_dist = dist; hl_thing = things_3d[t]; } } } } // Check Lines for (int a = 0; a < map.n_lines; a++) { if (!lines_3d[a].visible) continue; rect_t lrect = map.l_getrect(a); float dist = line_intersect(camera.position, camera.view, (float)lrect.x1() * SCALE_3D, (float)lrect.y1() * SCALE_3D, (float)lrect.x2() * SCALE_3D, (float)lrect.y2() * SCALE_3D); if (dist != -1 && dist < min_dist) { point3_t direction = camera.view - camera.position; point3_t hit_point(camera.position.x + (direction.x * dist), camera.position.y + (direction.y * dist), camera.position.z + (direction.z * dist)); sectors.push_back(map.l_getsector1(a)); sectors.push_back(map.l_getsector2(a)); // For all wallrects on the line for (int r = 0; r < lines_3d[a].rects.size(); r++) { if (!determine_line_side(lines_3d[a].rects[r]->verts[0].x, lines_3d[a].rects[r]->verts[0].y, lines_3d[a].rects[r]->verts[1].x, lines_3d[a].rects[r]->verts[1].y, camera.position.x, camera.position.y)) continue; float up = get_slope_height_point(lines_3d[a].rects[r]->verts[0], lines_3d[a].rects[r]->verts[1], hit_point); float lo = get_slope_height_point(lines_3d[a].rects[r]->verts[3], lines_3d[a].rects[r]->verts[2], hit_point); if (up >= hit_point.z && lo <= hit_point.z) { hl_thing = NULL; hl_wrect = lines_3d[a].rects[r]; min_dist = dist; break; } } } } // Check sectors for (int a = 0; a < ssects_3d.size(); a++) { if (!ssects_3d[a].visible) continue; point3_t direction = camera.view - camera.position; for (int b = 0; b < ssects_3d[a].flats.size(); b++) { // Get flat plane flatpoly_t *poly = ssects_3d[a].flats[b]; if (!(vector_exists(sectors, poly->parent_sector))) continue; plane_t plane; if (poly->part == PART_CEIL) plane = sector_info[poly->parent_sector].c_plane; else plane = sector_info[poly->parent_sector].f_plane; // Check side of plane float h = plane_height(plane, camera.position.x, camera.position.y); if (poly->part == PART_CEIL) { if (camera.position.z > h) continue; } if (poly->part == PART_FLOOR) { if (camera.position.z < h) continue; } float dist = dist_ray_plane(camera.position, direction, plane); if (dist <= min_dist && dist > 0.0f) { point3_t intersection(camera.position.x + (direction.x * dist), camera.position.y + (direction.y * dist), camera.position.z + (direction.z * dist)); bool in = true; DWORD start = gl_ssects[a].startseg; DWORD end = start + gl_ssects[a].n_segs; for (DWORD seg = start; seg < end; seg++) { if (!determine_seg_side(seg, intersection.x, intersection.y)) { in = false; break; } } if (in) { hl_wrect = NULL; hl_thing = NULL; hl_fpoly = poly; min_dist = dist; } } } } }