-5

Here, the blue line is the polygon-edge to be clipped. Yellow, line is the edge from a clipping polygon.

//Sutherland-Holdgman Line Clipping
#include "Line2d.h"
#include "Rectangle2d.h"
#include "Coordinates2d.h"
#include "Bits.h"
#include "Polygon2d.h"
#include <list>

typedef enum PointPosition 
{
    Left, Right
} PointPosition;

typedef enum LinePosition 
{
    CompletelyOut, CompletelyIn, ClippingCandidate, ClippingCandidateExtended
} LinePosition;

void EnumToString(LinePosition lnPos)
{
    switch(lnPos)
    {
    case CompletelyOut: 
        std::cout<<lnPos<<". CompletelyOut"; 
        break;
    case CompletelyIn: 
        std::cout<<lnPos<<". CompletelyIn"; 
        break;
    case ClippingCandidate: 
        std::cout<<lnPos<<". ClippingCandidate"; 
        break;
    case ClippingCandidateExtended: 
        std::cout<<lnPos<<". ClippingCandidateExtended"; 
        break;
    }
}

class ClippingPolygon2d
{
private:
    Rectangle2d rectangle;
    Polygon2d polygon;
    PointPosition CheckPointAgainstALine(Line2d line, Point2d pt)
    {
        double c = (((line.GetEnd().x - line.GetStart().x)
                    *(pt.y - line.GetStart().y)) 
                    -((line.GetEnd().y - line.GetStart().y)
                    *(pt.x - line.GetStart().x)));

        if(c>=0) return Left;
        else return Right;
    }
public:
    LinePosition CheckLineAgainstEdge(Line2d polygonEdge, Line2d line)
    {
        LinePosition linePosition;
        PointPosition startPoint = CheckPointAgainstALine(polygonEdge, line.GetStart());
        PointPosition endPoint = CheckPointAgainstALine(polygonEdge, line.GetEnd());

        if(startPoint==Left && endPoint==Left)
        {
            linePosition = CompletelyOut;
        }
        else if(startPoint==Right && endPoint==Right)
        {
            linePosition = CompletelyIn;
        }       
        else if(startPoint==Left && endPoint==Right)
        {
            linePosition = ClippingCandidateExtended;
        }
        else if(startPoint==Right && endPoint==Left)
        {
            linePosition = ClippingCandidate;
        }
        return linePosition;
    }
};



int main()
{
    Coordinates2d::ShowWindow("Sutherland-Hodgeman Line Clipping");

    ClippingPolygon2d clip;

    Line2d edge(Point2d(20,20), Point2d(160,140));
    Line2d ln(Point2d(100,120), Point2d(100,140));

    LinePosition lnPos = clip.CheckLineAgainstEdge(edge, ln);

    EnumToString(lnPos);

    Coordinates2d::Draw(edge, Blue);
    Coordinates2d::Draw(ln, Yellow);

    Coordinates2d::Wait();

    return 0;
}

enter image description here

user366312
  • 16,949
  • 65
  • 235
  • 452
  • why does this matter? – doron Jul 19 '15 at 23:05
  • Could you clarify what you mean? – juanchopanza Jul 19 '15 at 23:05
  • 1
    @KerrekSB `std::vector` – NaCl Jul 19 '15 at 23:08
  • 1
    This question indicates no research prior to posting. Also you claim 2 containers cannot work without indicating whether you tried them and, if so, what the problems apparently were. – underscore_d Jul 19 '15 at 23:08
  • 1
    I'm voting to close this question as off-topic because this question indicates no research prior to posting. – gsamaras Jul 19 '15 at 23:11
  • 1
    Is `array` == `std::array`? If so, how did this even compile? You didn't tell it which size to use, then you tried to allocate it at runtime from a `vector`, when the whole point is that `array` is statically allocated. Or is that just a placeholder line, in which case, that should be explicitly mentioned. – underscore_d Jul 19 '15 at 23:19
  • Your function returns a `vector`. Yet rather than simply assigning that to your `vector` in `main`, you have made up a "guess" about why this will not work, with no basis, then put an unrelated `array` line as the recipient of the function's `vector`, and are now asking SO what you should do. I am very confused about why you are confused. – underscore_d Jul 19 '15 at 23:32
  • This question has now changed completely twice. If you want to ask another question I suggest you ask another question. – Chris Drew Jul 20 '15 at 18:08

1 Answers1

1

You can use a std::vector without initializing its size and it supports random access. To add to an empty std::vector use push_back

std::vector<MyType> vec;
vec.push_back(MyType(10, 'a', "Hi"));
vec.push_back(MyType(20, 'b', "Hello"));
vec.push_back(MyType(30, 'c', "Bye"));

MyType t = vec[1];//20,'b', "Hello"...

Edit: This answer is for the question that was originally asked.

Chris Drew
  • 14,926
  • 3
  • 34
  • 54
  • Exactly, with a `std::tuple` in `Mytype`. However, consider if this question should be answered. – gsamaras Jul 19 '15 at 23:10
  • Why shouldn't it be answered? I've programmed in C++ for a while, I don't generally use [] with user types or stl but maybe because I'm used to writing code in a variety of languages. I didn't at first hand know the answer either (though I did have a good idea about the answer) Also, using [] isn't the same as using vector::at See:http://stackoverflow.com/questions/9376049/vectorat-vs-vectoroperator – ydobonebi Jul 19 '15 at 23:17
  • Speaking for myself at least, the argument for not answering such questions is that doing so promotes spoonfeeding answers that could be found on reference sites with extreme ease. – underscore_d Jul 19 '15 at 23:25