2016-04-20 2 views
1

Ich habe eine std::vector mit 3 Punkten (2D) mit Werten x> = 0 und x < = 512. Mit diesen 3 Punkten muss ich eine Auslosung berechnen, die alle diese 3 Punkte besteht. Here Sie sehen die 3 Punkte und den entsprechenden Kreis. Ich brauche eine Funktion, um die Punkte basierend auf einer Variablen zu interpolieren, die die Genauigkeit definiert (zB die Anzahl der Punkte dazwischen). Wenn es nicht klar ist: Ich arbeite in C++.Berechnung eines exakten Splines mit 3 gegebenen Punkten in 2D. C++

+0

Was haben Sie bisher versucht? –

+0

Zunächst einmal. Google. Viel davon. Das einzige, was ich herausgefunden habe, ist, dass ich mit catmull nahe an die Splines kommen könnte, die ich möchte. Aber es ist nicht genau genug. Meine mathematischen Fähigkeiten sind nicht die besten, so dass ich viel Mühe habe, eine Lösung zu finden. Ich habe keine Ahnung, wie ich überhaupt anfangen soll. Ich weiß, wie man einen Kreis berechnet. Das ist es. – Kiryu144

+0

Ist ein Spline eine Voraussetzung? Mit drei Punkten können Sie den Mittelpunkt und Radius des Kreises bestimmen. – molbdnilo

Antwort

1

Um Ihr Problem zu lösen, müssen Sie den Umkreis des Dreiecks und seinen Radius berechnen. Dann finden Sie Min X und Max X von Dreieckskoordinaten, dann berechnen Sie Delta zwischen MaxX - MinX und dividieren Delta zu Zahlen von Eingangspunkten. Dann in der Schleife iteriert man von minX zu maxX und berechnet Koordinaten mit der Kreisformel R^2 = (x - centerX)^2 + (y - centerY)^2. Unter einem kleinen Beispiel

#include <iostream> 
#include <vector> 
#include <math.h> 

template <typename T> 
class CPoint2D 
{ 
public: 
    CPoint2D(T _x, T _y) 
    : x(_x) 
    , y(_y) 
    {} 

    ~CPoint2D() 
    {} 

    const T& X() const { return x; } 
    const T& Y() const { return y; } 

private: 
    T x; 
    T y; 
}; 

typedef CPoint2D<float> CPoint2Df; 

bool GetCenterCircumscribedCircle(float x0, float y0, 
            float x1, float y1, 
            float x2, float y2, 
            float& centerX, float& centerY, float& radius) 
{ 
    if ((x0 == x1 && x1 == x2) || 
     (y0 == y1 && y1 == y2)) 
    { 
     return false; 
    } 

    float D = 2.0f * (y0 * x2 + y1 * x0 - y1 * x2 - y0 * x1 - y2 * x0 + y2 * x1); 
    centerX = ( y1 * x0 * x0 
       - y2 * x0 * x0 
       - y1 * y1 * y0 
       + y2 * y2 * y0 
       + x1 * x1 * y2 
       + y0 * y0 * y1 
       + x2 * x2 * y0 
       - y2 * y2 * y1 
       - x2 * x2 * y1 
       - x1 * x1 * y0 
       + y1 * y1 * y2 
       - y0 * y0 * y2)/D; 
    centerY = ( x0 * x0 * x2 
       + y0 * y0 * x2 
       + x1 * x1 * x0 
       - x1 * x1 * x2 
       + y1 * y1 * x0 
       - y1 * y1 * x2 
       - x0 * x0 * x1 
       - y0 * y0 * x1 
       - x2 * x2 * x0 
       + x2 * x2 * x1 
       - y2 * y2 * x0 
       + y2 * y2 * x1)/D; 

    radius = sqrt((x0 - centerX) * (x0 - centerX) + (y0 - centerY) * (y0 - centerY)); 

    return true; 
} 

void CalculatePointsOnCirle(const std::vector<CPoint2Df>& triVertexes, std::vector<CPoint2Df>& outPoints, float stride) 
{ 
    if (triVertexes.size() != 3) 
    { 
     return; 
    } 

    const CPoint2Df& v1 = triVertexes[0]; 
    const CPoint2Df& v2 = triVertexes[1]; 
    const CPoint2Df& v3 = triVertexes[2]; 

    float minX = std::min(v1.X(), v2.X()); 
    minX = std::min(minX, v3.X()); 

    float maxX = std::max(v1.X(), v2.X()); 
    maxX = std::max(maxX, v3.X()); 

    float deltaX = (maxX - minX)/stride; 

    float centerX; 
    float centerY; 
    float radius; 
    if (GetCenterCircumscribedCircle(v1.X(), v1.Y(), 
            v2.X(), v2.Y(), 
            v3.X(), v3.Y(), 
            centerX, centerY, radius)) 
    { 
     for (float x = minX; x < maxX; x += deltaX) 
     { 
      float y = sqrt(radius * radius - (x - centerX) * (x - centerX)); 

      outPoints.push_back(CPoint2Df(x, y)); 
     } 
    } 
} 

int main(int argc, const char * argv[]) 
{ 
    std::vector<CPoint2Df> triVertex = {CPoint2Df(0.0f, 0.0f), 
             CPoint2Df(256.0f, 256.0f), 
             CPoint2Df(512.0f, 0.0f)}; 

    std::vector<CPoint2Df> outPoints; 

    CalculatePointsOnCirle(triVertex, outPoints, 4); 

    for (unsigned int i = 0; i < outPoints.size(); ++i) 
    { 
     printf("p[%d]: (%f, %f)\n", i, outPoints[i].X(), outPoints[i].Y()); 
    } 

    return 0; 
} 
Verwandte Themen