Exemplo n.º 1
0
std::pair<Line, Line> Circle::commonOuterTangents(const Circle &c) const
{
    if (!(isValid && c.isValid) || (Vector(x, y).dist(Vector(c.x, c.y)) < fabs(r - c.r)))
    {
        return std::pair<Line, Line>(Line(false), Line(false));
    }

    Vector point;
    Circle pseudo;

    if (r < c.r)
    {
        point = Vector(x, y);
        pseudo = Circle(c.x, c.y, c.r - r);
    }
    else
    {
        point = Vector(c.x, c.y);
        pseudo = Circle(x, y, r - c.r);
    }

    Vector X1, X2;
    std::pair<Line, Line> ts = pseudo.tangents(point, &X1, &X2);
    if (!ts.second.isValid)
    {
        ts.first.moveToVector((point - pseudo.center()).getNormal() * r);
        ts.first.moveToVector(-(point - pseudo.center()).getNormal() * r);
    }
    else
    {
        ts.first .moveToVector((X1 - pseudo.center()).normalized() * min(r, c.r));
        ts.second.moveToVector((X2 - pseudo.center()).normalized() * min(r, c.r));
    }
    return ts;
}
Exemplo n.º 2
0
sb::IntersectionInfo sb::Intersection::get(const Polygon& a, const Circle& b) {
	assert(a.isSimple());
	assert(a.isConvex());
	assert(a.area() != 0);
	float t = std::numeric_limits<float>::max();
	auto aeCount = a.edgeCount();
	Vec2 normal;
	for (ptrdiff_t i = 0; i < aeCount; i++) {
		auto v = b.center() - a.edge(i).pointA();
		auto dt1 = dot(a.normal(i), v);
		auto dt2 = a.edge(i).containingLine().distanceFrom(b.center());
		if (dt1 <= 0 || dt2 <= b.radius()) {
			if (dt1 >= 0) {
				auto currentT = b.radius() - dt2;
				if (currentT < t) {
					t = currentT * currentT;
					normal = -a.normal(i);
				}
			}
			else {
				auto currentT = b.radius() + dt2;
				if (currentT < t) {
					t = currentT * currentT;
					normal = -a.normal(i);
				}
			}
		}
		else
			return IntersectionInfo(); //found separating axis

		auto n = a.point(i) - b.center();
		n.normalize();
		auto pt = b.center() + n * b.radius();
		bool foundNegative = false;
		float tEdge = std::numeric_limits<float>::lowest();
		Vec2 eNormal;
		for (ptrdiff_t j = 0; j < a.pointCount(); j++) {
			auto v = a.point(j) - pt;
			auto dt = dot(n, v);
			if (dt <= 0) {
				foundNegative = true;
				auto ln = Line(pt, pt + n.rotated90());
				auto currentT = ln.squaredDistanceFrom(a.point(j));
				if (currentT > tEdge) {
					tEdge = currentT;
					eNormal = n;
				}
			}
		}

		if (!foundNegative) //found separating axis
			return IntersectionInfo();
		if (tEdge < t) {
			t = tEdge;
			normal = eNormal;
		}
	}

	return IntersectionInfo(normal, sqrtf(t));
}
Exemplo n.º 3
0
IntersectionInfo Intersection::get(const Circle& a, const Circle& b) {
	auto v = a.center() - b.center();
	auto vlen = v.length();
	auto maxlen = a.radius() + b.radius();
	if (vlen > maxlen)
		return IntersectionInfo();
	else {
		v /= vlen;
		return IntersectionInfo(v, maxlen - vlen);
	}
}
Exemplo n.º 4
0
Polygon::Polygon(const Circle& circle, int points) {
    if(points <= 0) points = round(circle.radius() * 15);
    if(points < 3) points = 3;
    qreal max = 2 * M_PI;
    qreal inc = max / points;
    reserve(points);
    for(qreal d = 0; d < max; d += inc) {
        prepend(Point(cos(d) * circle.radius() + circle.center().x(),
                sin(d) * circle.radius() + circle.center().y()));
    }
}
Exemplo n.º 5
0
Option<Vec2> Intersection::get(const Ray& a, const Circle& b) {
	const auto f = a.point() - b.center();
	float _a = dot(a.direction(), a.direction());
	float _b = 2 * dot(f, a.direction());
	float _c = dot(f, f) - b.radius() * b.radius();
	float d = _b * _b - 4 * _a * _c;
	if (d <= 0) //if d == 0, this means that the ray is tangent to the circle... we don't wanna this Intersection...
		return nullptr;
	else {
		d = sqrtf(d);
		float t1 = (-_b - d) / (2 * _a);
		float t2 = (-_b + d) / (2 * _a);
		if (t1 >= 0) {
			if (t2 >= 0) {
				if (t1 < t2)
					return a.sampleAlongRay(t1);
				else
					return a.sampleAlongRay(t2);
			}
			else {
				return a.sampleAlongRay(t1);
			}
		}
		else {
			if (t2 >= 0)
				return a.sampleAlongRay(t2);
			else
				return nullptr;
		}
	}
}
Exemplo n.º 6
0
bool overlaps(Rect r, Circle c)
{
	return (overlaps(c.center(), r))
		|| (overlaps(r.topSide(), c))
		|| (overlaps(r.bottomSide(), c))
		|| (overlaps(r.leftSide(), c))
		|| (overlaps(r.rightSide(), c));
}
Exemplo n.º 7
0
int main(void) {
	Circle c;
	Rectangle r;

	c.center();  // call Figure.center() ... and Circle.draw();
	r.center();  // call Figure.center() ... and Rectangle.draw();
	return 0;
}
Exemplo n.º 8
0
const Neurons Neurons::operator & (const Circle<Micron> & circle) const
{
    CHECK_CONTAINER_MICROCIRCUIT_POINTER(*this);
    Neurons result(_microcircuit.lock());

    //! \todo Fix commented out code below. (TT)
    //result._description = _description + " within a radius of " + 
    //    boost::lexical_cast<const Word>(radius) + " from the center point ("
    //    + 
    //    boost::lexical_cast<const Word>(circle.center().x()) + ", " +
    //    boost::lexical_cast<const Word>(circle.center().z()) + ")";

    for (Neurons::const_iterator i = begin(); i != end(); ++i )
    {
		const Neuron & neuron = * i;
        Micron distance =
            std::sqrt((neuron.position().x() - circle.center().x()) * 
                      (neuron.position().x() - circle.center().x()) + 
                      (neuron.position().z() - circle.center().z()) * 
                      (neuron.position().z() - circle.center().z()));
#ifndef NDEBUG
        std::cout << "Distance:" << distance << std::endl;
#endif

        if (distance <= circle.radius())
        {
#ifndef NDEBUG
            std::cout << "Neuron a" << neuron.gid() << " at position " 
                      << neuron.position() << " is within a radius of " 
                      << circle.radius() << std::endl;
#endif
            result.insert(boost::const_pointer_cast
                <Neuron, const Neuron>(i.ptr()));
        }
#ifndef NDEBUG
        else
        {
            std::cout << "Neuron a" << neuron.gid() << " at position " 
                      << neuron.position() << " is NOT within a radius of " 
                      << circle.radius() << std::endl;
        }
#endif
    }
    return result;
}
Exemplo n.º 9
0
void Logic::drawCircle(SDL_Renderer *renderer, Circle circ, Uint32 color)
{
	Point center = (scale_ * circ.center().mirror_x()) + shift_;

	circleColor(renderer,
	            center.x, center.y,
	            scale_ * circ.radius(),
	            color);
}
Exemplo n.º 10
0
void Circle_push(lua_State *L, const Circle& s)
{
  lua_newtable(L);
  Vector2_push(L, s.center());
  lua_setfield(L, -2, "center");
  
  lua_pushnumber(L, s.radius());
  lua_setfield(L, -2, "radius");  
    
  lua_getglobal(L, "Circle");
  lua_setmetatable(L, -2);
  lua_getglobal(L, "Circle");
  lua_setfield(L, -2, "__index");      
}
Exemplo n.º 11
0
	// 接続ポイントを定義するヘルパー関数
	Point n(Circle& c){
		return Point(c.center().x + c.radius() * cos(M_PI / 2), c.center().y - c.radius() * sin(M_PI / 2));
	}
Exemplo n.º 12
0
	Point center(Circle& c){
		return Point(c.center().x, c.center().y);
	}
Exemplo n.º 13
0
	Point nw(Circle& c){
		return Point(c.center().x + c.radius() * cos(3 * M_PI / 4), c.center().y - c.radius() * sin(3 * M_PI / 4));
	}
Exemplo n.º 14
0
	Point e(Circle& c){
		return Point(c.center().x + c.radius() * cos(0), c.center().y - c.radius() * sin(0));
	}
Exemplo n.º 15
0
bool Intersection::test(const Circle& a, const Circle& b) {
	float d = distance(a.center(), b.center());
	float maxlen = a.radius() + b.radius();
	return d <= maxlen;
}
Exemplo n.º 16
0
bool overlaps(Line line, Circle circ)
{
	return (line.sqr_dist_to(circ.center()) <= sqr(circ.r));
}
Exemplo n.º 17
0
bool overlaps(Point p, Circle c)
{
	return (p.sqr_dist_to(c.center()) <= sqr(c.r));
}
Bbox Circle::bounding_box()const{
	Circle temp = *this;
	Bbox schmox{temp.center(), temp.radius()};
	return schmox;
}