void Face::move(int a, int b) { rectangle::move(a, b); l_eye->move(a, b); r_eye->move(a, b); mouth->move(a, b); }
const collision line_line(const line &a, const line &b) { collision r; auto m1 = (a.position.y - a.end.y) / (a.position.x - a.end.x); auto m2 = (b.position.y - b.end.y) / (b.position.x - b.end.x); auto b1 = a.position.y - m1*a.position.x; auto b2 = b.position.y - m2*b.position.x; float x = (b2 - b1) / (m1 - m2); float y = m1*x + b1; if ((a.position.x - a.end.x) == 0) // if a is a vertical line, then there is only 1 possible x value. { x = a.position.x; y = m2 *x + b2; } if ((b.position.x - b.end.x) == 0) { x = b.position.x; y = m1 *x + b1; } r.contact = { x, y }; point p(r.contact); r.result = circle_point(circle(a.mid(), a.length() / 2), p).result && circle_point(circle(b.mid(), b.length() / 2), p).result; if (m1 == m2) { r.result = false; return r; } return r; }
int intersec(const line& l1, const line& l2, point& res){ assert(!(l1.v == point())); assert(!(l2.v == point())); if (vp(l1.v,l2.v) == point()){ if (vp(l1.v, l1.p - l2.p) == point()) return 2; // same return 0; // parallel } point n = vp(l1.v,l2.v); point p = l2.p - l1.p; if (sgn(sp(n,p))) return 0; // skew ld t; if (sgn(n.x)) t = (p.y * l2.v.z - p.z * l2.v.y) / n.x; else if (sgn(n.y)) t = (p.z * l2.v.x - p.x * l2.v.z) / n.y; else if (sgn(n.z)) t = (p.x * l2.v.y - p.y * l2.v.x) / n.z; else assert(false); res = l1.p + l1.v * t; assert(l1.on(res)); assert(l2.on(res)); return 1; // intersects }
std::tuple<bool, float> line::intersect(const line &other) const { //from http://stackoverflow.com/a/1968345 double p0_x = m_origin.x(); double p0_y = m_origin.y(); double p1_x = m_target.x(); double p1_y = m_target.y(); double p2_x = other.origin().x(); double p2_y = other.origin().y(); double p3_x = other.target().x(); double p3_y = other.target().y(); double s1_x = p1_x - p0_x; double s1_y = p1_y - p0_y; double s2_x = p3_x - p2_x; double s2_y = p3_y - p2_y; if ((-s2_x * s1_y + s1_x * s2_y) == 0) { return std::make_tuple(false, 0.0f); } double s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y); double t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y); double w = 0.1; if (s >= 0-w && s <= 1+w && t >= 0-w && t <= 1+w) { // Collision detected return std::make_tuple(true, t); } return std::make_tuple(false, 0.0f); }
bool plane::isInPlane(const line& l)const { auto dot = norm_.dotProduct(l.dir()); if (!floatEqual(dot, 0.0f)) return false; return isInPlane(l.start()); }
point intersect(line & l, segment s) { double da = fabs(l.dist(s.a)), db = fabs(l.dist(s.b)); if (da + db < eps) { return s.a; } else { double t = da / (da + db); return s.a + (s.b - s.a) * t; } }
void draw_line ( line l ) { XDrawLine ( m_display, m_window_id, m_gc, l.point1().x(), l.point1().y(), l.point2().x(), l.point2().y() ); }
std::pair<bool, vector3> plane::intersection(const line& l)const { auto ret = std::make_pair<bool, vector3>(false, vector3()); if (isInPlane(l)) return ret; ret.first = true; auto t = (norm_.dotProduct(point_) - norm_.dotProduct(l.start())) / (norm_.dotProduct(l.dir())); ret.second = l.start() + t * l.dir(); return ret; }
pt operator&(const line &l1, const line &l2) { double d = l1.a * l2.b - l1.b * l2.a; assert(fabs(d) > eps); pt res( (l1.b * l2.c - l1.c * l2.b) / d, (l1.a * l2.c - l1.c * l2.a) / -d ); assert(l1.side(res) == 0); assert(l2.side(res) == 0); return res; }
// gets the intersection between two lines (not segments) // ot : the other line // if they are equal, returns nan // if they are parallel, returns inf // else, returns the x on which the intersection occurs double intersection (const line<cood> & ot) const { double a[2] = {slope(), ot.slope()}; double b[2] = {intercept(a[0]), ot.intercept(a[1])}; if (abs(a[0]-a[1]) < eps) { if (abs(b[0]-b[1]) < eps) return 0./0.; return 1./0.; } else { debug("%.2f/%.2f", b[0]-b[1], a[1]-a[0]); return (b[0]-b[1])/(a[1]-a[0]); } return true; }
const double line::getDistFromLine(const line& l) const { assert(this->is_Set() && l.is_Set()); double this_ori = this->getOrientation(); double other_ori = l.getOrientation(); double diff_ori = fabs(this_ori - other_ori); double d = (point2Line(*this, l.m_endp1) <= point2Line(*this, l.m_endp2)) ? point2Line(*this, l.m_endp1): point2Line(*this, l.m_endp2); return d + 1.5*diff_ori; // 1.5 is a magic number return 0; }
bool overlap(line another){ if(type !=another.type){ return false; } if(type==0){ if (y0!=another.y0){ return false; } } if(type==1){ if (x0!=another.x0){ return false; } } if(type==2 ){ if (y0-x0!=another.y0-another.x0){ return false; } } if(type==3 ){ if (y0+x0!=another.y0+another.x0){ return false; } } line possible_merge_line=merge(another); if(possible_merge_line.len()<=len()+another.len()){ return true; } return false; }
int main () { scanf("%d", &n); for (int i = 0; i < n; i++) { debug("%d: ", i); scanf("%lld %lld", &v.s.x, &v.s.y); scanf("%lld %lld", &v.t.x, &v.t.y); scanf("%lld %lld", &a[0].x, &a[0].y); scanf("%lld %lld", &a[2].x, &a[2].y); a[1].x = a[0].x; a[1].y = a[2].y; a[3].x = a[2].x; a[3].y = a[0].y; if (v.s.inside({a[0], a[1], a[2], a[3]}) || v.t.inside({a[0], a[1], a[2], a[3]})) { debug("1"); printf("T\n"); continue; } int j = 0; for (j = 0; j < 4; j++) { line<> u(a[j], a[(j+1)%4]); double x = v.intersection(u); if (x == 0./0.) { debug("e"); if (v.contains(u.s.x) || v.contains(u.t.x) || u.contains(v.s.x) || u.contains(v.t.x)) break; } else if (v.contains(x) && u.contains(x)) { debug("c"); break; } else { debug("[%.1f]", x); } } if (j == 4) printf("F\n"); else printf("T\n"); } }
void drawLine(line l) { if (l.isDiameter()) { glBegin(GL_LINE_STRIP); glVertex2d(l.getLeft().getX(), l.getLeft().getY()); glVertex2d(l.getRight().getX(), l.getRight().getY()); glEnd(); return; } double middle = l.getCenter().arg() + M_PI; double deflection = atan(1/l.getRadius()); double start = middle - deflection; double end = middle + deflection; drawArc(l.getCenter(), l.getRadius(), start, end); }
void check_cross(const pt ¢, const double &r, const line &l, int need_cnt) { vector<pt> res = cross(cent, r, l); printf("check circle&line\n"); for (int i = 0; i < sz(res); i++) { printf(" %.2lf %.2lf\n", res[i].x, res[i].y); assert(l.side(res[i]) == 0); assert(fabs((cent - res[i]).dist2() - r * r) < eps); } assert(sz(res) == need_cnt); }
vector<pt> cross(const pt ¢er, double r, const line &l) { double di = l.distz(center); double d2 = l.norm2(); assert(fabs(d2) > eps); pt mid = center + pt(l.a, l.b) * (-di / d2); #ifdef DEBUG assert(l.side(mid) == 0); #endif double s = r * r - di * di / d2; if (s < -eps) return vector<pt>(); if (fabs(di * di - r * r * d2) < eps) return vector<pt>(1, mid); pt off = pt(-l.b, l.a) * sqrt(s / d2); assert(fabs(off.dist2() - s) < eps); vector<pt> res; res.pb(mid + off); res.pb(mid - off); return res; }
void closest(const line& l1,const line& l2, point& p1,point& p2){ if (vp(l1.v,l2.v) == point()){ p1 = l1.p; p2 = l2.p - l1.v * /*BOXNEXT*/ (sp(l1.v,l2.p - l1.p) / l1.v.dist2()); return; } point p = l2.p - l1.p; ld t1 = ( sp(l1.v,p) * l2.v.dist2() - sp(l1.v,l2.v) * sp(l2.v,p) ) / vp(l1.v,l2.v).dist2(); ld t2 = ( sp(l2.v,l1.v) * sp(l1.v,p) - sp(l2.v,p) * l1.v.dist2() ) / vp(l2.v,l1.v).dist2(); p1 = l1.p + l1.v * t1; p2 = l2.p + l2.v * t2; assert(l1.on(p1)); assert(l2.on(p2)); }
pReal line::shortestDistance( line a ) { pVector cP = direction.cross( a.direction ); if( direction.normalise() == a.direction.normalise() ) { return ( a.sample( plane( position, direction ).collision( a ) ) - position ).length(); } if( cP != cP ) { return 0; } return fabs( cP.dot( a.position - position ) / cP.length( ) ); }
bool linePolyCollision( line & testLine, polygon & testPoly) { // 2 intersections should be a collision polyIterator itr = testPoly.begin(); int collCounter = 0; int pointCount = 1; while( itr != testPoly.end() ) { point collPoint = testLine.checkIntersection( *itr ); if ( testLine.isPointOnLine( collPoint ) ) { collCounter++; } ++itr; pointCount++; } if( collCounter > 1 ) { return true; } return false; }
bool is_laying(construction_line<T1> const & src, line<Pt2> const & target) { typedef typename quan::meta::binary_op< T1, quan::meta::minus,typename Pt2::value_type >::type value_type; construction_line<value_type> cons_target(target.from,target.to - target.from); std::pair<bool,quan::two_d::vect<value_type> > intersect_pt = intersect(src,cons_target); if (!intersect_pt.first) { return false; } typename value_type mag_target = target.length(); quan::two_d::vect<value_type> v1 (target.from - intersect_pt.second); if (magnitude(v1) > mag_target) return false; quan::two_d::vect<value_type> v2 (target.to -intersect_pt.second); if(magnitude(v2) > mag_target) return false; return true; }
bool line<2>::intersection_point(const line<2>& ln, point<2>& p) const { // returns true if an intersection with line exists, // and p as the intersection point if ((*this) == ln) { p.x() = p.y() = 0.0; return true; } if (parallelQ(ln)) return false; p.x() = (ln.b * d - b * ln.d) / (ln.a * b - a * ln.b); if (fabs(b) > EPSILON) // test for vertical line p.y() = - (a * p.x() + d) / b; else p.y() = - (ln.a * p.x() + ln.d) / ln.b; return true; }
int main () { scanf("%d", &n); while (n--) { scanf("%d %d %d %d", &l.s.x, &l.s.y, &l.t.x, &l.t.y); scanf("%d %d", &v[0].x, &v[0].y); scanf("%d %d", &v[2].x, &v[2].y); v[1].x = v[0].x; v[1].y = v[2].y; v[3].x = v[2].x; v[3].y = v[0].y; bool ok = 0; if (l.s.inside({v[0], v[1], v[2], v[3]}) || l.t.inside({v[0], v[1], v[2], v[3]})) ok = 1; else { for (int j = 0; j < 4; j++) ok |= l.intersects(line<ll>(v[j], v[(j+1)%4])); } if (ok) printf("T\n"); else printf("F\n"); } }
point line::checkIntersection( const line & line2 ) const { // now the real work begins // lets try this using determinants // converting to form Ax + By = C float A1 = _y2 - _y1; float B1 = _x1 - _x2; float C1 = ( A1 * _x1 ) + ( B1 * _y1 ); float A2 = line2.getPoint2().y - line2.getPoint1().y; float B2 = line2.getPoint1().x - line2.getPoint2().x; float C2 = ( A2 * line2.getPoint1().x ) + ( B2 * line2.getPoint1().y ); float det = ( A1 * B2 ) - ( A2 * B1 ); // parallel test if ( det == 0 ) point( 0, 0 ); float x_intersect = ( ( B2 * C1 ) - ( B1 * C2 ) ) / det; float y_intersect = ( ( A1 * C2 ) - ( A2 * C1 ) ) / det; return point( x_intersect, y_intersect ); }
result lineIntersectsPolygon(line & l, polygon & P, size_t sz, int k) { int n = (sz + k - 1) / k; double minDist = numeric_limits<double>::max(); int minPoint = -1; unordered_set<int> points; if (n <= 50) { point prev = P->get(0); for (size_t i = k; i < sz; i += k) { point cur = P->get(i); segment s = {prev, cur}; if (lineIntersectsSegment(l, s)) { points.insert(i - k); } else { double dist = fabs(l.dist(cur)); if (minDist > dist) { minDist = dist; minPoint = i; } } prev = cur; } } else { auto ind = lineIntersectsPolygon(l, P, sz, 2 * k); if (ind.points.size() != 0) { for (int i : ind.points) { point p1 = P->get(i - k); point p2 = P->get(i); segment s = {p1, p2}; if (lineIntersectsSegment(l, s)) { points.insert((i - k + sz) % sz); } else { points.insert(i); } } } else { int i = ind.closest; point p1 = P->get(i - 2 * k); point p2 = P->get(i - k); point p3 = P->get(i); point p4 = P->get(i + k); point p5 = P->get(i + 2 * k); point pts[] = {p1, p2, p3, p4, p5}; segment segs[] = {{p1, p2}, {p2, p3}, {p3, p4}, {p4, p5}}; for (int j = 0; j < 4; j++) { if (lineIntersectsSegment(l, segs[j])) { points.insert((i + k * (j - 2) + sz) % sz); } } for (int j = 0; j < 5; j++) { double dist = fabs(l.dist(pts[j])); if (minDist > dist) { minDist = dist; minPoint = (i + k * (j - 2) + sz) % sz; } } } } return {points, minPoint}; }
line<double,3> Sector::sector_to_clas(const line<double,3>& l) const { return line<double,3>{ this->sector_to_clas(l.point()), this->sector_to_clas(l.direction()) }; }
DWORD WINAPI Thd_Match(PVOID lParam) { UNREFERENCED_PARAMETER(lParam); XLIB_TRY { const DWORD start_time = GetTickCount(); mlog << "\r\n\r\n------------------------匹配工作开始------------------------\r\n"; if(g_file_sig) { ifstream file(g_filename.c_str(), ios::in | ios::binary); if(!file) { file.close(); mlog << "无法打开或读取指定的特征码文件:" << g_filename; return 0; } file.seekg(0, ios::end); const unsigned int filelen = file.tellg(); if(filelen == 0) { file.close(); mlog << "指定的特征码文件:" << g_filename << "内容为空"; return 0; } file.seekg(0, ios::beg); g_signature.clear(); g_signature.reserve(filelen); file.read((char*)g_signature.end()._Ptr, filelen); g_signature.append(g_signature.end()._Ptr, filelen); file.close(); if(g_create_atom) { const DWORD atom_start_time = GetTickCount(); line sig(g_signature); const line atoms(signaturematcher::create_atom_by_script(sig)); if(!atoms.empty()) { string atomname(g_filename); const size_t pos = atomname.find_last_of('.'); if(pos != string::npos) { atomname.erase(pos); } atomname.append(".atom"); ofstream of(atomname.c_str(), ios::out | ios::binary); of.write((const char*)atoms.c_str(), atoms.size()); of.close(); mlog << "成功解析并写入中间原子文件(" << (int)(GetTickCount() - atom_start_time) << "ms):"<< atomname << "\r\n"; } } } if(g_blk.start() == nullptr) { pe tmppe(GetModuleHandle(nullptr)); g_blk = tmppe.GetImage(); } g_blks.clear(); g_blks.push_back(g_blk); g_report.clear(); signaturematcher::analy_script(g_signature, match_routine, nullptr); if(g_file_sig && !g_report.empty()) { const DWORD h_start_time = GetTickCount(); xmsg s64, s32, s16, s8, sp; const size_t max_name_len = 46; for(auto rep : g_report) { signaturematcher::REPORT_VALUE& rv = rep.second.values; xmsg* lpmsg = nullptr; xmsg var; switch(rv.t) { case 'q': lpmsg = &s64; var << rv.q; break; case 'd': lpmsg = &s32; var << rv.d; break; case 'w': lpmsg = &s16; var << rv.w; break; case 'b': lpmsg = &s8; var << rv.b; break; case 'p': lpmsg = &sp; var << rv.p; break; default: lpmsg = &s64; var << rv.q; break; } if(!(*lpmsg).empty()) (*lpmsg) << "\r\n"; (*lpmsg) << " " << rep.first; if(rep.first.size() < max_name_len) { (*lpmsg).append(max_name_len - rep.first.size(),' '); } (*lpmsg) << " = 0x" << var << ","; if(!rep.second.note.empty()) (*lpmsg) << " //" << rep.second.note; } xmsg hmsg; hmsg << "#pragma once\r\n\r\n"; if(g_cx11) { if(!s64.empty()) hmsg << "enum : unsigned __int64\r\n {\r\n" << s64 << "\r\n };\r\n\r\n"; if(!sp.empty()) hmsg << "enum : size_t\r\n {\r\n" << sp << "\r\n };\r\n\r\n"; if(!s32.empty()) hmsg << "enum : unsigned __int32\r\n {\r\n" << s32 << "\r\n };\r\n\r\n"; if(!s16.empty()) hmsg << "enum : unsigned __int16\r\n {\r\n" << s16 << "\r\n };\r\n\r\n"; if(!s8.empty()) hmsg << "enum : unsigned __int8\r\n {\r\n" << s8 << "\r\n };"; } else { hmsg << "enum\r\n {"; if(!s64.empty()) hmsg << "\r\n" << s64; if(!sp.empty()) hmsg << "\r\n" << sp; if(!s32.empty()) hmsg << "\r\n" << s32; if(!s16.empty()) hmsg << "\r\n" << s16; if(!s8.empty()) hmsg << "\r\n" << s8; hmsg << "\r\n };"; } string hname(g_filename); const size_t pos = hname.find_last_of('.'); if(pos != string::npos) { hname.erase(pos); } hname.append(".h"); ofstream of(hname.c_str(), ios::out | ios::binary); of.write((const char*)hmsg.c_str(), hmsg.size()); of.close(); mlog << "成功写入H文件(" << (int)(GetTickCount() - h_start_time) << "ms):" << hname; } const DWORD end_time = GetTickCount(); mlog << "\r\n------------------------匹配工作结束------------------------\t" << (int)(end_time - start_time) << "ms"; return 0; } XLIB_CATCH { ; } mlog << "----------------匹配工作出现未知异常,失败!----------------"; return 0; }
#include "catch.hpp" #define PFS_GRIOTTE_SOURCE #include <pfs/griotte/line.hpp> using line = pfs::griotte::line<int>; SCENARIO("Rect constructors", "[line]") { GIVEN("A rect") { WHEN("created with default constructor") { line l; THEN("all attributes are zero") { REQUIRE(l.get_start_point().get_x() == 0); REQUIRE(l.get_start_point().get_y() == 0); REQUIRE(l.get_end_point().get_x() == 0); REQUIRE(l.get_end_point().get_y() == 0); } } AND_WHEN("created by default copy constructor") { line l1; line l2(l1); THEN("all attributes are zero") { REQUIRE(l2.get_start_point().get_x() == 0); REQUIRE(l2.get_start_point().get_y() == 0); REQUIRE(l2.get_end_point().get_x() == 0); REQUIRE(l2.get_end_point().get_y() == 0); } }
__HOST____DEVICE__ scalar line<point2D, const point2D&>::nearestDist ( const line<point2D, const point2D&>& e, point2D& thisPt, point2D& edgePt ) const { vector2D u = end()-start(); vector2D v = e.end()-e.start(); vector2D w = start()-e.start(); scalar d = u.perp(v); if (Foam::mag(d) > VSMALL) { scalar s = v.perp(w) / d; if (s <= SMALL) { thisPt = start(); } else if (s >= (1-SMALL)) { thisPt = end(); } else { thisPt = start()+s*u; } scalar t = u.perp(w) / d; if (t <= SMALL) { edgePt = e.start(); } else if (t >= (1-SMALL)) { edgePt = e.end(); } else { edgePt = e.start()+t*v; } } else { // Parallel lines. Find overlap of both lines by projecting onto // direction vector (now equal for both lines). scalar edge0 = e.start() & u; scalar edge1 = e.end() & u; bool edgeOrder = edge0 < edge1; scalar minEdge = (edgeOrder ? edge0 : edge1); scalar maxEdge = (edgeOrder ? edge1 : edge0); const point2D& minEdgePt = (edgeOrder ? e.start() : e.end()); const point2D& maxEdgePt = (edgeOrder ? e.end() : e.start()); scalar this0 = start() & u; scalar this1 = end() & u; bool thisOrder = this0 < this1; scalar minThis = min(this0, this1); scalar maxThis = max(this1, this0); const point2D& minThisPt = (thisOrder ? start() : end()); const point2D& maxThisPt = (thisOrder ? end() : start()); if (maxEdge < minThis) { // edge completely below *this edgePt = maxEdgePt; thisPt = minThisPt; } else if (maxEdge < maxThis) { // maxEdge inside interval of *this edgePt = maxEdgePt; thisPt = nearestDist(edgePt).rawPoint(); } else { // maxEdge outside. Check if minEdge inside. if (minEdge < minThis) { // Edge completely envelops this. Take any this point and // determine nearest on edge. thisPt = minThisPt; edgePt = e.nearestDist(thisPt).rawPoint(); } else if (minEdge < maxThis) { // minEdge inside this interval. edgePt = minEdgePt; thisPt = nearestDist(edgePt).rawPoint(); } else { // minEdge outside this interval edgePt = minEdgePt; thisPt = maxThisPt; } } } return Foam::mag(thisPt - edgePt); }
bool lineIntersectsSegment(line & l, segment & s) { double da = l.dist(s.a), db = l.dist(s.b); return !((da > eps && db > eps) || (da < -eps && db < -eps)); }
experimental_double line::kink_theta(const line& l) { return forward_axis_.kink_theta(l.forward_axis()); }