bool GLine::contains(double x, double y) const { if (transformed) { return stanfordcpplib::getPlatform()->gobject_contains(this, x, y); } double x0 = getX(); double y0 = getY(); double x1 = x0 + dx; double y1 = y0 + dy; double tSquared = LINE_TOLERANCE * LINE_TOLERANCE; if (dsq(x, y, x0, y0) < tSquared) { return true; } if (dsq(x, y, x1, y1) < tSquared) { return true; } if (x < std::min(x0, x1) - LINE_TOLERANCE) { return false; } if (x > std::max(x0, x1) + LINE_TOLERANCE) { return false; } if (y < std::min(y0, y1) - LINE_TOLERANCE) { return false; } if (y > std::max(y0, y1) + LINE_TOLERANCE) { return false; } if (floatingPointEqual(x0 - x1, 0) && floatingPointEqual(y0 - y1, 0)) { return false; } double u = ((x - x0) * (x1 - x0) + (y - y0) * (y1 - y0)) / dsq(x0, y0, x1, y1); return dsq(x, y, x0 + u * (x1 - x0), y0 + u * (y1 - y0)) < tSquared; }
static bool containsGLine(GLine line, double x, double y) { double x0, y0, x1, y1, tsq, u; x0 = line->x; y0 = line->y; x1 = x0 + line->width; y1 = y0 + line->height; tsq = LINE_TOLERANCE * LINE_TOLERANCE; if (dsq(x, y, x0, y0) < tsq) return true; if (dsq(x, y, x1, y1) < tsq) return true; if (x < fmin(x0, x1) - LINE_TOLERANCE) return false; if (x > fmax(x0, x1) + LINE_TOLERANCE) return false; if (y < fmin(y0, y1) - LINE_TOLERANCE) return false; if (y > fmax(y0, y1) + LINE_TOLERANCE) return false; if ((float) (x0 - x1) == 0 && (float) (y0 - y1) == 0) return false; u = ((x - x0) * (x1 - x0) + (y - y0) * (y1 - y0)) / dsq(x0, y0, x1, y1); return dsq(x, y, x0 + u * (x1 - x0), y0 + u * (y1 - y0)) < tsq; }
bool GLine::contains(double x, double y) const { if (transformed) return pp->contains(this, x, y); double x0 = getX(); double y0 = getY(); double x1 = x0 + dx; double y1 = y0 + dy; double tSquared = LINE_TOLERANCE * LINE_TOLERANCE; if (dsq(x, y, x0, y0) < tSquared) return true; if (dsq(x, y, x1, y1) < tSquared) return true; if (x < min(x0, x1) - LINE_TOLERANCE) return false; if (x > max(x0, x1) + LINE_TOLERANCE) return false; if (y < min(y0, y1) - LINE_TOLERANCE) return false; if (y > max(y0, y1) + LINE_TOLERANCE) return false; if ((float) (x0 - x1) == 0 && (float) (y0 - y1) == 0) return false; double u = ((x - x0) * (x1 - x0) + (y - y0) * (y1 - y0)) / dsq(x0, y0, x1, y1); return dsq(x, y, x0 + u * (x1 - x0), y0 + u * (y1 - y0)) < tSquared; }
bool FlatLine::intersect_czero(const fixed r, FlatPoint &i1, FlatPoint &i2) const { const fixed _dx = dx(); const fixed _dy = dy(); const fixed dr = dsq(); const fixed D = cross(); fixed det = sqr(r) * dr - sqr(D); if (negative(det)) // no solution return false; det = sqrt(det); const fixed inv_dr = fixed_one / dr; i1.x = (D * _dy + sgn(_dy, _dx) * det) * inv_dr; i2.x = (D * _dy - sgn(_dy, _dx) * det) * inv_dr; i1.y = (-D * _dx + fabs(_dy) * det) * inv_dr; i2.y = (-D * _dx - fabs(_dy) * det) * inv_dr; return true; }
bool FlatLine::intersect_czero(const double r, FlatPoint &i1, FlatPoint &i2) const { const auto _dx = dx(); const auto _dy = dy(); const auto dr = dsq(); const auto D = cross(); auto det = Square(r) * dr - Square(D); if (det < 0) // no solution return false; det = sqrt(det); const auto inv_dr = 1. / dr; i1.x = (D * _dy + copysign(_dx, _dy) * det) * inv_dr; i2.x = (D * _dy - copysign(_dx, _dy) * det) * inv_dr; i1.y = (-D * _dx + fabs(_dy) * det) * inv_dr; i2.y = (-D * _dx - fabs(_dy) * det) * inv_dr; return true; }
fixed FlatLine::d() const { return sqrt(dsq()); }