static void sCubic(LinearPathConsumer& t, const Pointf& p1, const Pointf& p2, const Pointf& p3, const Pointf& p4, double qt, int lvl) { if(lvl < 16) { PAINTER_TIMING("Cubic approximation"); Pointf d = p4 - p1; double q = d.x * d.x + d.y * d.y; if(q >= 1e-30) { Pointf d2 = p2 - p1; Pointf d3 = p3 - p1; double u1 = (d2.x * d.x + d2.y * d.y) / q; double u2 = (d3.x * d.x + d3.y * d.y) / q; if(u1 <= 0 || u1 >= 1 || u2 <= 0 || u2 >= 1 || SquaredDistance(u1 * d, d2) > qt || SquaredDistance(u2 * d, d3) > qt) { Pointf p12 = Mid(p1, p2); Pointf p23 = Mid(p2, p3); Pointf p34 = Mid(p3, p4); Pointf p123 = Mid(p12, p23); Pointf p234 = Mid(p23, p34); Pointf div = Mid(p123, p234); sCubic(t, p1, p12, p123, div, qt, lvl + 1); sCubic(t, div, p234, p34, p4, qt, lvl + 1); return; } } } t.Line(p4); }
void CircumCenter(Simplex* simplex, double* out) { Vertex* a,* b,* c,* d; GetFaceVerticies(simplex, 0, &a, &b, &c, &d); double b_a[3] , c_a[3] , d_a[3], cross1[3], cross2[3], cross3[3], mult1[3] , mult2[3] , mult3[3], sum[3]; double denominator; VertexSub(b->m_Point, a->m_Point, b_a); VertexSub(c->m_Point, a->m_Point, c_a); VertexSub(d->m_Point, a->m_Point, d_a); CrossProduct(b_a, c_a, cross1); CrossProduct(d_a, b_a, cross2); CrossProduct(c_a, d_a, cross3); VertexByScalar(cross1, SquaredDistance(d_a), mult1); VertexByScalar(cross2, SquaredDistance(c_a), mult2); VertexByScalar(cross3, SquaredDistance(b_a), mult3); VertexAdd(mult1, mult2, sum); VertexAdd(mult3, sum , sum); denominator = 2*ScalarProduct(b_a, cross3); VertexByScalar(sum, 1/(double)(denominator), out); VertexAdd(out, a->m_Point, out); }
bool CollisionManager::CircleToPixels(double cx, double cy, double cr, const CollisionPixelData* pixels, double px, double py) const { bool result = false; if ( CircleToRect(cx, cy, cr, px, py, pixels->GetWidth(), pixels->GetHeight()) ) { // Obtenemos rectangulo del solapamiento. double rx = 0; double ry = 0; double rw = 0; double rh = 0; OverlappingRect(cx - cr, cy - cr, cr * 2, cr * 2, px, py, pixels->GetWidth(), pixels->GetHeight(), &rx, &ry, &rw, &rh); // Obtenemos primer pixel del rectangulo. uint32 coordXFirstPixel1 = ( uint32 )( rx - px ); uint32 coordYFirstPixel1 = ( uint32 )( ry - py ); // Comprobamos la colision for (uint16 i = 0; i < rw; i++) for (uint16 j = 0; j < rh; j++) if ( pixels->GetData(coordXFirstPixel1 + i, coordYFirstPixel1 + j) ) if ( SquaredDistance(cx, cy, rx + i, ry + j) <= Pow(cr, 2) ) result = true; } return result; }
int main() { int T; scanf("%d", &T); for (int t = 1; t <= T; ++t) { int N; scanf("%d", &N); for (int i = 0; i < N; ++i) scanf("%d%d", xLocation + i, yLocation + i); for (int first = 0; first < N; ++first) { for (int second = first + 1; second < N; ++second) { int squaredDist = SquaredDistance(first, second); if (squaredDist <= MaxSquaredDistance) { double dist = sqrt(squaredDist); distance[first][second] = dist; distance[second][first] = dist; } else { distance[first][second] = INF; distance[second][first] = INF; } } } for (int k = 0; k < N; ++k) { for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { distance[i][j] = std::min(distance[i][j], distance[i][k] + distance[k][j]); } } } double maximum = 0; for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { maximum = std::max(maximum, distance[i][j]); } } printf("Case #%d:\n", t); if (maximum > POSSIBLE_MARGIN) printf("Send Kurdy\n\n"); else printf("%.4f\n\n", maximum); } }
bool CollisionManager::CircleToRect(double cx, double cy, double cr, double rx, double ry, double rw, double rh) const { double outx = 0; double outy = 0; ClosestPointToRect(cx, cy, rx, ry, rw, rh, &outx, &outy); return SquaredDistance(cx, cy, outx, outy) <= Pow(cr, 2); }
void Painter::DoSvgArc(const Pointf& rr, double xangle, int large, int sweep, const Pointf& p1, const Pointf& p0) { Pointf r(fabs(rr.x), fabs(rr.y)); Xform2D m = Xform2D::Rotation(-xangle); Pointf d1 = m.Transform(0.5 * (p0 - p1)); Pointf pr = r * r; Pointf p = d1 * d1; double check = p.x / pr.x + p.y / pr.y; if(check > 1) r *= sqrt(check); m.x /= r.x; m.y /= r.y; Pointf q0 = m.Transform(p0); Pointf q1 = m.Transform(p1); double d = SquaredDistance(q0, q1); double sfactor_sq = 1.0 / d - 0.25; if(sfactor_sq < 0) sfactor_sq = 0; double sfactor = sqrt(sfactor_sq); if(sweep == large) sfactor = -sfactor; Pointf c(0.5 * (q0.x + q1.x) - sfactor * (q1.y - q0.y), 0.5 * (q0.y + q1.y) + sfactor * (q1.x - q0.x)); double theta = Bearing(q0 - c); double th_sweep = Bearing(q1 - c) - theta; if(th_sweep < 0 && sweep) th_sweep += 2 * M_PI; else if(th_sweep > 0 && !sweep) th_sweep -= 2 * M_PI; m = Xform2D::Rotation(xangle); m.x *= r; m.y *= r; m = Xform2D::Translation(c.x, c.y) * m; DoArc0(theta, th_sweep, m); }
NAMESPACE_UPP static void sQuadratic(LinearPathConsumer& t, const Pointf& p1, const Pointf& p2, const Pointf& p3, double qt, int lvl) { if(lvl < 16) { PAINTER_TIMING("Quadratic approximation"); Pointf d = p3 - p1; double q = Squared(d); if(q > 1e-30) { Pointf pd = p2 - p1; double u = (pd.x * d.x + pd.y * d.y) / q; if(u <= 0 || u >= 1 || SquaredDistance(u * d, pd) > qt) { Pointf p12 = Mid(p1, p2); Pointf p23 = Mid(p2, p3); Pointf div = Mid(p12, p23); sQuadratic(t, p1, p12, div, qt, lvl + 1); sQuadratic(t, div, p23, p3, qt, lvl + 1); return; } } } t.Line(p3); }
bool ETHShaderManager::BeginLightPass(ETHSpriteEntity *pRender, const ETHLight* light, const float maxHeight, const float minHeight, const float lightIntensity, const ETHSpriteEntity *pParent, const bool drawToTarget) { if (!light || !pRender->IsApplyLight()) return false; Vector3 v3LightPos; if (pParent) v3LightPos = pParent->GetPosition() + light->pos; else v3LightPos = light->pos; const Vector2 &v2Size = pRender->GetCurrentSize(); const float size = Max(v2Size.x, v2Size.y); const float distance = SquaredDistance(pRender->GetPosition(), v3LightPos); const float radius = (light->range + size); if (distance > radius * radius) return false; m_currentProfile->BeginLightPass(pRender, v3LightPos, v2Size, light, maxHeight, minHeight, lightIntensity, drawToTarget); m_parallaxManager.SetShaderParameters(m_video, m_video->GetVertexShader(), pRender->GetPosition(), drawToTarget); return true; }
int PreprocessOccluders(const tmatrix &invCameraMat) { if (!GetInstancesCount(ZOccluderBox)) return 0; PROFILER_START(PreprocessOccluders); tvector3 campos = invCameraMat.V4.position; tvector3 camdir = invCameraMat.V4.dir; FActiveOccluder* pActiveOccluder = &gActiveOccluders[0]; tvector4 viewPoint = vector4(campos.x, campos.y, campos.z, 0); tvector4 viewDir = vector4(camdir.x, camdir.y, camdir.z, 0); float sqrFar = 1000.0f * 1000.0f; float sqrDist; int i; gNbActiveOccluders = 0; gOccluderBoxes.clear(); ZOccluderBox *pocc = (ZOccluderBox*)FirstInstanceOf(ZOccluderBox); while (pocc) { addDynamicOccluder(pocc->GetTransform()); pocc = (ZOccluderBox*)NI(pocc); } for (unsigned int ju = 0;ju<gOccluderBoxes.size(); ju++) { // todo : frustum culling of the occluder (except far plane) // todo : compute solid angle to reorder occluder accordingly FOccluderBox *obox = &gOccluderBoxes[ju]; //= oboxiter.Get(); // check for far plane const tvector3 &oboxcenter = obox->mCenter;//pocc->GetTransform()->GetWorldMatrix().position; sqrDist= SquaredDistance(viewPoint, oboxcenter); if (sqrDist > sqrFar) { continue; } // check for near plane if (DotProduct(tvector3(viewDir), tvector3(oboxcenter - viewPoint)) < 0.0f) { continue; } // select planes of the occluder box that lies on the viewing direction // todo : reduce to 3 planes instead of 6 (due to box symetry) float invSqrDist = 1.0f/sqrDist;//Rcp(sqrDist); pActiveOccluder->mSolidAngle = 0.0f; BoxSilhouette silhouette; silhouette.vertices = &obox->mVertex[0]; for (i=0; i<6; i++) { tvector4 dir= obox->mVertex[FaceVertexIndex[i][0]]; dir -= viewPoint; float vdotp = silhouette.dots[i] = DotProduct(obox->mPlanes[i], dir); // compute the maximum solidAngle of the box : -area * v.p/d.d pActiveOccluder->mSolidAngle = Max(-obox->mVertex[i].w * vdotp * invSqrDist, pActiveOccluder->mSolidAngle); } // exit if the occluder is not relevant enough if (pActiveOccluder->mSolidAngle < gMinSolidAngle) continue; int nPlanes = 0; tvector4* pPlanes = &pActiveOccluder->mPlanes[0]; // find silhouette tvector4 vertices[12]; int nVertices = silhouette.findSilhouette(vertices); // create a plane with a edge of the occluder and the viewpoint for (i=0; i<nVertices; i+=2) { //tplane plan(campos, vertices[i], vertices[i+1]); tvector3 v1 = vertices[i]; v1 -= viewPoint; tvector3 v2 = vertices[i+1]; v2 -= viewPoint; v1.Normalize(); v2.Normalize(); *pPlanes = CrossProduct(v1, v2); pPlanes->Normalize(); pPlanes->w = - DotProduct(*pPlanes, vertices[i]); pPlanes++; nPlanes ++; } if (gAddNearPlane) { for (int i=0; i<6; i++) { if (silhouette.dots[i] < 0.0f) { pActiveOccluder->mPlanes[nPlanes] = obox->mPlanes[i]; nPlanes++; } } } pActiveOccluder->mNbPlanes = nPlanes; pActiveOccluder++; gNbActiveOccluders++; if (gNbActiveOccluders >= gMaxCandidateOccluders) break; } if (gNbActiveOccluders) { qsort(gActiveOccluders, gNbActiveOccluders, sizeof(FActiveOccluder), compareOccluder); if (gNbActiveOccluders > gMaxActiveOccluders) gNbActiveOccluders = gMaxActiveOccluders; } PROFILER_END(); return gNbActiveOccluders; }
bool CollisionManager::CircleToCircle(double x1, double y1, double r1, double x2, double y2, double r2) const { return SquaredDistance(x1, y1, x2, y2) <= Pow(r1 + r2, 2); }