/* * The Circle main thread */ int main(int argc, char** argv) { double radius; cout << "Please enter the radius of a circle: "; cin >> radius; Circle* circle = new Circle(radius); cout << "The area of this circle is: " << circle->getArea() << endl; cout << "The diameter of this circle is: " << circle->getDiameter() << endl; cout << "The circumference of this circle is: " << circle->getCircumference() << endl; return 0; }
/* a[i] 和 b[i] 分别是第i条切线在O1和O2上的切点 */ int getTangents (Circle o1, Circle o2, Point* a, Point* b) { int cnt = 0; if (o1.r < o2.r) { swap(o1, o2); swap(a, b); } double d2 = getLength(o1.o - o2.o); d2 = d2 * d2; double rdif = o1.r - o2.r, rsum = o1.r + o2.r; if (d2 < rdif * rdif) return 0; if (dcmp(d2) == 0 && dcmp(o1.r - o2.r) == 0) return -1; double base = getAngle(o2.o - o1.o); if (dcmp(d2 - rdif * rdif) == 0) { a[cnt] = o1.point(base); b[cnt] = o2.point(base); cnt++; return cnt; } double ang = acos( (o1.r - o2.r) / sqrt(d2) ); a[cnt] = o1.point(base+ang); b[cnt] = o2.point(base+ang); cnt++; a[cnt] = o1.point(base-ang); b[cnt] = o2.point(base-ang); cnt++; if (dcmp(d2 - rsum * rsum) == 0) { a[cnt] = o1.point(base); b[cnt] = o2.point(base); cnt++; } else if (d2 > rsum * rsum) { double ang = acos( (o1.r + o2.r) / sqrt(d2) ); a[cnt] = o1.point(base+ang); b[cnt] = o2.point(base+ang); cnt++; a[cnt] = o1.point(base-ang); b[cnt] = o2.point(base-ang); cnt++; } return cnt; }
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)); }
void initShape(){ Circle* shape = new Circle(); shape->setPosition(-0.5,0,0); cir.push_back(shape); shape = new Circle(); shape->setPosition(0.5,0,0); cir.push_back(shape); shape = new Circle(); shape->setPosition(0,0.5,0); cir.push_back(shape); }
bool Circle::within(FigureType type, const double * coordinates, unsigned size) { if (type == FigureType::rect) { if (size < 4) return false; Rectangle polygon; polygon.setCoord(coordinates, size); if ( !(center.getX() - radius >= polygon.getX() && center.getX() + radius <= polygon.getX() + polygon.getWidth()) ) return false; if ( !(center.getY() - radius >= polygon.getY() && center.getY() + radius <= polygon.getY() + polygon.getHeight()) ) return false; return true; } else if (type == FigureType::circle) { if (size < 3) return false; Circle polygon; polygon.setCoord(coordinates, size); double distX = polygon.getX() - center.getX(); double distY = polygon.getY() - center.getY(); double radiusDifference = polygon.getRadius() - radius; if (radiusDifference < 0) return false; if ( !(distX*distX + distY*distY <= radiusDifference*radiusDifference) ) return false; return true; } return false; }
int main() { /* request autodetection */ int gdriver = DETECT, gmode; /* initialize graphics mode */ initgraph(&gdriver, &gmode, ""); drawAxis(WHITE); Point o = {0,0}; Circle c = {o,130}; c.bressenPlot(RED); Point p[NUM_VERTICES]= { Point{129,10}, Point{28,10}, Point{-28,10}, Point{-129,10}, Point{129,-10}, Point{28,-10}, Point{-28,-10}, Point{-129,-10}, Point{10,129}, Point{10,28}, Point{10,-129}, Point{10, -28}, Point{-10,129}, Point{-10,28}, Point{-10,-129}, Point{-10, -28} }; Circle inCircle = {o, 30}; inCircle.bressenPlot(RED); seedFill4(o, RED, RED); double theta = -(3.142857*10)/180.0; do { for(int i = 0; i<16; i+=2) { Line l = Line {p[i], p[i+1]}; l.plotLineDDA(YELLOW); } delay(15); for(int i = 0; i<16; i+=2) { Line l = Line {p[i], p[i+1]}; l.plotLineDDA(BLACK); } rotateAll(p, theta); c.bressenPlot(RED); } while(1); getch(); closegraph(); return 0; }
bool test(const Circle& circle, const RotatedRectangle& rect) { /* Let E be the centre of the rectangle (i.e. the origin of its local coordinate frame). Define the regions A-I as below, where the central region E has the dimensions of the rectangle. A │ B │ C ──┼───┼── D │ E │ F ──┼───┼── G │ H │ I Check distance to side in regions: B, D, F, and H. Check distance to corner in regions: A, C, G, and I. Region E is always an intersection. Note: This diagram and the circle are symmetric, so we can use absolute values to simplify the comparisons. i.e. hw B │ C ──┼── hh E │ F */ Transform2D trans = rect.getTransform(); Transform2D pos = trans.worldToLocal({circle.centre(0), circle.centre(1), 0}); double hw = 0.5 * rect.getSize()(0); double hh = 0.5 * rect.getSize()(1); double r = circle.radius; double x = std::abs(pos(0)); double y = std::abs(pos(1)); if (x < hw && y < hh) { // E return true; } if (x < hw && y > hh) { // B return y < hh + r; } if (x > hw && y < hh) { // F return x < hw + r; } // if (x > hw && y > hh) { // C arma::vec2 cornerDiff = { hw - x, hh - y }; return arma::norm(cornerDiff) < r; // } }
void MainWindow::on_actionCircle_triggered() { Circle *c = new Circle(); m_canvas->addVisualEntity(c); DrawDialog *d = DrawDialogFactory::CreateDrawDialog(this, c); d->exec(); delete d; ActiveSelection::getInstance().deselectAll(); c->setSelected(true); DrawCommand *comm = new DrawCommand(c); comm->addtoCommandStack(); }
int main(void) { Point A, B, C; Circle circumCircle; while(scanf("%lf %lf %lf %lf %lf %lf", &A._x, &A._y, &B._x, &B._y, &C._x, &C._y) > 0) { circumCircle = Circle(A, B, C); circumCircle.printCompactEquation(); circumCircle.printExpandEquation(); printf("\n"); } return 0; }
bool H2Geodesic::contains(const H2Point & p) const { if (isCircleInDiskModel()) { Circle C; getCircleInDiskModel(C); return C.contains(p.getDiskCoordinate()); } else { PlanarLine L; getLineInDiskModel(L); return L.contains(p.getDiskCoordinate()); } }
bool Polygon::Intersects(const Circle& other) const { const Polygon& B = *this; if (Intersects(other.Position())) return true; // If the center of the circle is inside the polygon for (int i = 0; i < B.NumPoints(); ++i) { if (other.Intersects(Segment(B[i], B[i + 1]))) return true; } return false; }
bool Collision::Circle_vs_Circle(const Circle& a, const Circle& b, Vector *v) { const Vector apos = a.getPosition(); Vector diff = b.getPosition() - apos; const float r = a.radius + b.radius; bool c = diff.isLength2DIn(r); if(c && v) { diff.setLength2D(a.radius); *v = apos + diff; } return c; }
void Collision::CircleToCircle(Circle &a, Circle &b) { ManifoldCtoC m; Vector2D diff = a.getPosition().subtract(b.getPosition());//!< Origin of position subtracting origin of other position; float dist = diff.magnitude();//!< Gets the length between the two origins. float sumOfRadii = a.getRadius() + b.getRadius();//!< Sum of the radii if (dist > sumOfRadii)//!< Exits early if no collision detection. { return; } //!send circles and the calculated distance to the manifold. m.Initialize(a,b, dist);//!< Initializes the manifold. m.ApplyImpulse(a,b); }
/* // Ret should = 1 for the base case void factorial( int &fact, int n ){ if( n == 0 ){ fact = 1; } else{ fact = fact * n; n--; if( n > 1 ) factorial( fact, n ); } } */ int main(){ // int n = 4; // int ret = 1; Shape *s = new Shape(); Shape *t = new Dounut(); Circle *u = new Dounut("Joe"); s->printStuff(); t->printStuff(); u->printStuff(); // factorial( ret, n ); // std::cout << "\nfactorial is " << ret << std::endl; }
bool Circle::operator>=(Circle C1) { if (radius >= C1.getRadius()) return true; else return false; }
bool CircleContainer::insert(const Circle& circle) { ScopedWriteLock lock(read_write_mutex_); auto result = circle_container_.insert(map<wstring, Circle>::value_type(circle.getCircleID(), circle)); onCircleinserted(circle); return result.second; }
Polygon::Polygon(const Circle& circle, int num) : FConvex(circle.Position()), m_vPoints(num) { real angle = -2*pi/num; real sint = sin(angle); real cost = cos(angle); Point current(-circle.Radius(), 0); for (int i = 0; i < num; ++i) { m_vPoints[i] = current; current.Rotate(sint, cost); } }
Size3D Circle::getIntersection(Circle circle) { float circlesDistance = _location.getDistance(circle.getLocation()); float intersection = _radius + circle.getRadius() - circlesDistance; return Size3D(intersection,intersection); /*Point2D2 circleLocation = circle.getLocation(); float xDistance = fmaxf(_location.getX(),circleLocation.getX()) - fminf(_location.getX(),circleLocation.getX()); float xIntersection = _radius + circle.getRadius() - xDistance; if(xIntersection > 0) { float yDistance = fmaxf(_location.getY(),circleLocation.getY()) - fminf(_location.getY(),circleLocation.getY()); float yIntersection = _radius + circle.getRadius() - yDistance; return Size2D(xIntersection,yIntersection); } return Size2D(xIntersection);*/ }
void Circle::intersect(const Circle& other, he::PrimitiveList<vec2>& outIntersections) const { float d(length(m_Position - other.getPosition())); if (d > m_Radius + other.m_Radius) { return; } else if (d < fabs(m_Radius - other.m_Radius)) { return; } else { //line connecting 2 points = const float& x1(m_Position.x), x2(other.m_Position.x), y1(m_Position.y), y2(other.m_Position.y), r1(m_Radius), r2(other.m_Radius); float d2(lengthSqr(other.m_Position - m_Position)); float xPart1( (x2 + x1) / 2.0f + ((x2 - x1) * (sqr(r1) - sqr(r2))) / (2.0f * d2)); float xPart2( ((y2 - y1) / (2.0f * d2)) * sqrtf( (sqr(r1 + r2) - d2) * (d2 - sqr(r2 - r1)))); float yPart1( (y2 + y1) / 2.0f + ((y2 - y1) * (sqr(r1) - sqr(r2))) / (2.0f * d2)); float yPart2( ((x2 - x1) / (2.0f * d2)) * sqrtf( (sqr(r1 + r2) - d2) * (d2 - sqr(r2 - r1)))); outIntersections.add( vec2(xPart1 + xPart2, yPart1 - yPart2) ); outIntersections.add( vec2(xPart1 - xPart2, yPart1 + yPart2) ); } }
bool TestSegCircle_Below ( Segment3d const & S, Circle const & circle ) { Vector A = S.getBegin(); Vector B = S.getEnd(); Vector const & C = circle.getCenter(); if(A.y > B.y) std::swap(A,B); if(C.y > B.y) { return false; } else if(C.y < A.y) { return Overlap2d::TestSegCircle(S,circle); } else { if(A.y == B.y) { return Overlap2d::TestSegCircle(S,circle); } else { float param = (C.y - A.y) / (B.y - A.y); Vector D = A + (B - A) * param; return Overlap2d::TestSegCircle( Segment3d(D,B), circle ); } } }
int main(void) { //Create vector shape with an initial size of 6 std::vector <Shape *> shape(6); //Initialize vector with Shapes shape.push_back(new Circle("green", 10)); shape.push_back(new Rectangle("red", 8, 6)); shape.push_back(new Triangle("yellow", 8, 4)); shape.push_back(new Triangle("black", 4, 10)); shape.push_back(new Circle("orange", 5)); shape.push_back(new Rectangle("blue", 3, 7)); std::cout << std::endl << "Printing all shapes..." << std::endl << std::endl; //Process each element in vector 'shape' //'unsigned int' due to comparison warning for (unsigned int i = 0; i < shape.size(); ++i) { Shape *shapePtr = dynamic_cast<Shape *> (shape[i]); if (shapePtr != 0) shapePtr->print(); } std::cout << std::endl << "Printing only circles..." << std::endl << std::endl; //'unsigned int' due to comparison warning for (unsigned int i = 0; i < shape.size(); ++i) { Circle *circlePtr = dynamic_cast<Circle *> (shape[i]); if (circlePtr != 0) circlePtr->print(); } //Loop through list of shape pointers and delete each object //Only needed if using ARRAY, not VECOTR. // for (unsigned int i = 0; i < shape.size(); ++i) // delete shape[i]; std::cout << std::endl; return 0; }
bool Sector::Intersects(const Circle& circle) const { //Intersections performed fast to slow. //FAST: //If bounding circle and argument circle do not intersect: no. //If initial point intersects circle: yes. //If terminal point intersects circle: yes. //If central point intersects circle: yes. //SLOW: //If the angle between the circle and the sector is in-between the start and end angles (mapped to [0, 2pi]): yes. //If the circle intersects the initial line: yes. //If the circle intersects the terminal line: yes. //Otherwise: no. double my_x = this->GetX(); double my_y = this->GetY(); double your_x = circle.GetX(); double your_y = circle.GetY(); double radius_sum = this->GetRadius() + circle.GetRadius(); if(a2de::Point::GetDistance(my_x, my_y, your_x, your_y) > radius_sum) return false; if(this->GetStartPoint().Intersects(circle)) return true; if(this->GetEndPoint().Intersects(circle)) return true; if(Point(this->GetPosition()).Intersects(circle)) return true; double angle = Vector2D::GetFacingAngle(circle.GetPosition(), this->GetPosition()); //Map return values to [0, 360] double sA = this->GetStartAngle(); if(sA > Math::A2DE_PI) { sA -= Math::A2DE_2PI; } double eA = this->GetEndAngle(); if(eA > Math::A2DE_PI) { eA -= Math::A2DE_2PI; } bool within_sector = (sA <= angle && angle <= eA); if(within_sector) return true; Line sl(this->GetPosition(), this->GetStartPoint().GetPosition()); Line tl(this->GetPosition(), this->GetEndPoint().GetPosition()); if(sl.Intersects(circle)) return true; if(tl.Intersects(circle)) return true; return false; }
void circles_test(u32 n, u32 size) { std::cout << "Circles(" << n << "):" << std::endl; Array<Circle> circles; circles.reserve(n); { BlockMeasure m(" creation"); for (u32 i=0; i<n; ++i) { f32 x = ((f32)rand()/RAND_MAX)*size; f32 y = ((f32)rand()/RAND_MAX)*size; f32 r = ((f32)rand()/RAND_MAX)*5 + 0.1f; circles.add(Vec2{x, y}, r); } } u32 overlaps = 0; { BlockMeasure m(" update + collide"); for (u32 i=0; i<n; ++i) { f32 dx = ((f32)rand()/RAND_MAX)*0.5f; f32 dy = ((f32)rand()/RAND_MAX)*0.5f; Circle* c = circles.getAtPos(i); c->setCenter(c->getCenter()+Vec2{dx, dy}); } for (u32 i=0; i<n; ++i) { Circle* c1 = circles.getAtPos(i); for (u32 j=i+1; j<n; ++j) { Circle* c2 = circles.getAtPos(j); if (c1->overlaps(*c2)) ++overlaps; } } } std::cout << " overlaps: " << overlaps << std::endl; fast_vector<unsigned int> destruction_order; destruction_order.reserve(n); randomPickN(destruction_order, n, n); { grynca::BlockMeasure m(" removal"); for (u32 i=0; i<destruction_order.size(); ++i) { u32 circle_id = destruction_order[i]; circles.removeAtPos(circle_id); } } }
bool Intersector::intersect(const Circle &circle0, const Circle &circle1) { if (are_equal(circle0.get_centre().x(), circle1.get_centre().x()) && are_equal(circle0.get_centre().y(), circle1.get_centre().y())) { return are_equal(circle0.get_radius(), circle1.get_radius()); } return is_less_equal( Distance::compute(circle0.get_centre(), circle1.get_centre()), (circle0.get_radius() + circle1.get_radius())); }
void initMesh(int nc, int p){ graph.clear(); set.clear(); initial.clear(); constraint.clear(); numCir = nc; poly = p; Circle cir = CXY(1); for (int i = 0; i < numCir; ++i){ VT radius = 1 + 2.0 * (float)i/numCir; Circle tcir = cir.dilate( log( radius ) ); for (int j = 0; j < poly; ++j ){ VT theta = TWOPI * j / poly; theta += (rtheta * i * PI/poly); Point p = Round::pnt_cir(tcir, theta ); set.add( p ); } } bool bSwitch = false; for (int j = 0; j < poly; ++j){ int a = j; int b = ( j < poly - 1 ) ? j + 1 : 0; int c = j + poly; //cout << a << " " << b << " " << c << endl; initial.add( Rigid2( set[c], set[a], set[b], bSwitch ) ); bSwitch = !bSwitch; } bSwitch = false; for (int i = 1; i < numCir-1; ++i){ for (int j = 0; j < poly; ++j ){ int a = i * poly + j; int b = a + poly; int c = j < poly -1 ? a + 1 : i * poly; int d = j < poly -1 ? a - poly + 1 : (i-1)*poly; constraint.add( Rigid3( set[b], set[a], set[d], set[c], bSwitch ) ); bSwitch = !bSwitch; } } }
int Circle::intersects(Circle &other, Point *i) const { // http://local.wasp.uwa.edu.au/~pbourke/geometry/2circle/ // (Mathworld's solution uses inconvenient coordinates) // // This is rearranged to perform the sqrts after the number // of intersection points is known, so they can be omitted // if the points are not needed or if the circles don't intersect. float dsq = (center - other.center).magsq(); if (dsq == 0) { // Concentric circles: no points or all points return 0; } float r0sq = radius_sq(); float r1sq = other.radius_sq(); float t = (r0sq - r1sq + dsq); float asq = t * t / (4 * dsq); float hsq = r0sq - asq; int n; if (hsq < 0) { return 0; } else if (hsq == 0) { n = 1; } else { n = 2; } if (i) { float x0 = center.x; float y0 = center.y; float x1 = other.center.x; float y1 = other.center.y; float a_over_d = sqrtf(asq / dsq); float h_over_d = sqrtf(hsq / dsq); float x2 = x0 + (x1 - x0) * a_over_d; float y2 = y0 + (y1 - y0) * a_over_d; i[0].x = x2 + (y1 - y0) * h_over_d; i[0].y = y2 - (x1 - x0) * h_over_d; if (n == 2) { i[1].x = x2 - (y1 - y0) * h_over_d; i[1].y = y2 + (x1 - x0) * h_over_d; } } return n; }
int main() { float radius, area, perimeter; // create circle instance Circle myCircle; // ask the user for radius cout << "Enter radius of a circle to get its area and perimeter: \n"; cin >> radius; myCircle.setRadius(radius); // get the results and print them out myCircle.printResults(); return 0; }
// 两圆相交 int getCircleCircleIntersection(Circle& C1, Circle& C2, vector<Point>& sol) { double d = Length(C1.c - C2.c); if(dcmp(d) == 0) { if(dcmp(C1.r - C2.r) == 0) return -1; // 重合,无穷多交点 return 0; } if(dcmp(C1.r + C2.r - d) < 0) return 0; if(dcmp(fabs(C1.r-C2.r) - d) > 0) return 0; double a = angle(C2.c - C1.c); double da = acos((C1.r*C1.r + d*d - C2.r*C2.r) / (2*C1.r*d)); Point p1 = C1.point(a-da), p2 = C1.point(a+da); sol.push_back(p1); if(p1 == p2) return 1; sol.push_back(p2); return 2; }
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)); }
//-------------------------------------------------------------- void ofApp::setup(){ numLeaves = 40; for(int i=0; i<numLeaves; i++){ Leaf tempLeaf; tempLeaf.setup(); leaves.push_back(tempLeaf); } numCircles = 100; for(int i=0; i<numCircles; i++){ Circle tempCircle; tempCircle.setup(); circles.push_back(tempCircle); } ofSetRectMode(OF_RECTMODE_CENTER); ofEnableSmoothing(); ofEnableAlphaBlending(); }