Will th ebelow code work andif not please help me fix it Bas

Will th ebelow code work andif not please help me fix it. Basically I am taking in a line of code and we are supposed to use fail state to figure out which kind of format the line will follow. It will either follow one of two formats. So I am trying to say if while reading in it hits something that doesn\'t work it must be the OTHER kind of line. Am I using clear and fail functions correctly? It is important that when it fails the next value being read in goes into the place where it failed reading in.

So if it tries to read something in and it fails the next value read in should go into THAT value and not the next one.

Solution

Reasons:

i) You cannot start read vertexOne or vertexTwo, before you open the stream

ii) Set up your file reading in streams based on the layout of your files, so what you can do is to have if-else

    statements that select the right stream setup bases on the filname.   

Program:

#include \"Triangle.h\"
#include \"Graphics.h\"
#include \"Shape.h\"
#include <cmath>
using namespace std;
Triangle::Triangle()
{
}
Triangle::Triangle(Point pt1, Point pt2, Point pt3, Color color) {
    vertexOne = pt1;
    vertexTwo = pt2;
    vertexThree = pt3;
    vertexOneColor = color;
    vertexTwoColor = color;
    vertexThreeColor = color;
}
Triangle::Triangle(Point pt1, Color col1, Point pt2, Color col2,    Point pt3, Color col3)
{
    vertexOne = pt1;
    vertexOneColor = col1;
    vertexTwo = pt2;
    vertexTwoColor = col2;
    vertexThree = pt3;
    vertexThreeColor = col3;
}
void Triangle::setColor(Color col)
{
    vertexOneColor = col;
    vertexTwoColor = col;
    vertexThreeColor = col;
}
void Triangle::setVertexOne(Point pt)
{
    vertexOne = pt;
}
void Triangle::setVertexTwo(Point pt)
{
    vertexTwo = pt;
}
void Triangle::setVertexThree(Point pt)
{
    vertexThree = pt;
}
Point Triangle::getVertexOne()
{
    return vertexOne;
}
Point Triangle::getVertexTwo()
{
    return vertexTwo;
}
Point Triangle::getVertexThree()
{
    return vertexThree;
}
void Triangle::setVertexOneColor(Color col)
{
    vertexOneColor = col;
}
void Triangle::setVertexTwoColor(Color col)
{
    vertexTwoColor = col;
}
void Triangle::setVertexThreeColor(Color col) {
    vertexThreeColor = col;
}
Color Triangle::getVertexOneColor()
{
    return vertexOneColor;
}
Color Triangle::getVertexTwoColor()
{
    return vertexTwoColor;
}
Color Triangle::getVertexThreeColor()
{
    return vertexThreeColor;
}
void Triangle::read(istream& ins)
{
    // These are values of the each vertex and color
    Point vertex1;
    Point vertex2;
    Point vertex3;
    Color col;
    Color vertex1Color;
    Color vertex2Color;
    Color vertex3Color;

    ins >> vertex1 >> vertex1Color >> vertex2 >> vertex2Color >> vertex3;

    // Checks whether ins has entered a fail state of not
    if (ins.fail())
    {
        ins.clear();
         Triangle(vertex1, vertex2, vertex3, vertex1Color);
    }
    else
   {
        ins >> vertex3Color;
         Triangle(vertex1, vertex1Color, vertex2, vertex2Color,vertex3, vertex3Color);
    }
}
void Triangle::write(ostream& outs)
{
    Point out_vertex1 = getVertexOne();
    Point out_vertex2 = getVertexTwo();
    Point out_vertex3 = getVertexThree();
    Color out_vertex1Color = getVertexOneColor();
    Color out_vertex2Color = getVertexTwoColor();
    Color out_vertex3Color = getVertexThreeColor();

    outs << out_vertex1 << \" \" << out_vertex1Color << \" \" << out_vertex2 << \" \" << out_vertex2Color << \" \" << out_vertex3 << \" \" << out_vertex3Color;
}
istream& operator >> (istream& ins, Triangle& tri)
{
    tri.read(ins);
    return ins;
}
ostream& operator << (ostream& outs, Triangle tri)
{
    tri.write(outs);
    return outs;
}
void Triangle::draw (Graphics& drawer)
{
    int xa = checkRange(getVertexOne().getX());
    int ya = checkRange(getVertexOne().getY());
    int xb = checkRange(getVertexTwo().getX());
    int yb = checkRange(getVertexTwo().getY());
    int xc = checkRange(getVertexThree().getX());
    int yc = checkRange(getVertexThree().getY());
    float red1 = (float)(getVertexOneColor().getRed()/255.0);
    float green1 = (float)(getVertexOneColor().getGreen()/255.0);
    float blue1 = (float)(getVertexOneColor().getBlue()/255.0);
    float red2 = (float)(getVertexTwoColor().getRed()/255.0);
    float green2 = (float)(getVertexTwoColor().getGreen()/255.0);
    float blue2 = (float)(getVertexTwoColor().getBlue()/255.0);
    float red3 = (float)(getVertexThreeColor().getRed()/255.0);
    float green3 = (float)(getVertexThreeColor().getGreen()/255.0);
    float blue3 = (float)(getVertexThreeColor().getBlue()/255.0);

    float a01, a02, a03, b01, b02, b03, c01, c02, c03, beta1, beta2, beta3;
    float lowX = 0, lowY = 0, highX = 0, highY = 0, i, j, R, G, B, t1, t2, t3;
    float triangleArea = 0;
   a01 = (float)(ya - yb);
    a02 = (float)(ya - yc);
    a03 = (float)(yb - yc);
    b01 = (float)(xb - xa);
    b02 = (float)(xc - xa);
    b03 = (float)(xc - xb);
    c01 = -(a01 * (xa+xb)+ b01 * (ya + yb)) / 2;
    c02 = -(a02 *(xa+xc) + b02 * (ya + yc)) / 2;
    c03 = -(a03 *(xb+xc) + b03 * (yb + yc)) / 2;

    if (evalFunc(xc,yc, (int)a01, (int)b01, (int)c01) < 0)
    {
        a01 = a01 * (-1);
        b01 = b01 * (-1);
        c01 = c01 * (-1);
    }
    if (evalFunc(xb,yb, (int)a02, (int)b02, (int)c02) < 0)
   {
        a02 = a02 * (-1);
        b02 = b02 * (-1);
        c02 = c02 * (-1);
    }
    if (evalFunc(xa,ya, (int)a03, (int)b03, (int)c03) < 0)
    {
        a03 = a03 * (-1);
        b03 = b03 * (-1);
        c03 = c03 * (-1);
    }
    if (xa > xb)
    {
       highX = (float)xa;
    }
     else
       {
           highX = (float)xb;
       }
    if (xc > highX)
     {
         highX = (float)xc;
      }
    if (ya > yb)
     {
         highY = (float)ya;
      }
      else
          {
               highY = (float)yb;
           }
       if (yc > highY)
          {
            highY = (float)yc;
           }
    if (xa < xb)
       {
           lowX = (float)xa;
        }
     else
       {
           lowX = (float)xb;
       }
     if (xc < lowX)
         {
            lowX = (float)xc;
         }
    if (ya < yb)
         {
            lowY = (float)ya;
         }
       else
          {
              lowY = (float)yb;
           }
    if (yc < lowY)
          {
             lowY = (float)yc;
          }

    triangleArea = triArea(xa, ya, xb, yb, xc, yc);

    for (i = lowX; i < (highX+1); i++) {
        for (j = lowY; j < (highY+1); j++) {
            t1 = (float)evalFunc((int)i,(int)j, (int)a01, (int)b01, (int)c01);
            t2 = (float)evalFunc((int)i,(int)j, (int)a02, (int)b02, (int)c02);
            t3 = (float)evalFunc((int)i,(int)j, (int)a03, (int)b03, (int)c03);
            if ((t1 >= 0)&&(t2 >= 0)&&(t3 >= 0)){
                t1 = triArea(xb, yb, xc, yc, (int)i, (int)j);
                t2 = triArea(xc, yc, xa, ya, (int)i, (int)j);
                t3 = triArea(xa, ya, xb, yb, (int)i, (int)j);
                beta1 = triArea(xb, yb, xc, yc, (int)i, (int)j)/triangleArea;
                beta2 = triArea(xc, yc, xa, ya, (int)i, (int)j)/triangleArea;
                beta3 = triArea(xa, ya, xb, yb, (int)i, (int)j)/triangleArea;
                R = (beta1*red1 + beta2*red2 + beta3*red3) * 255;
                G = (beta1*green1 + beta2*green2 + beta3*green3) * 255;
                B = (beta1*blue1 + beta2*blue2 + beta3*blue3) * 255;
                drawer.setPixel((int)i,(int)j,Color((int)R,(int)G,int(B)));
            }
        }
    }
}

int Triangle::checkRange(int val)
{
    if (val < 0)
        return 0;
    else if (val > DIMENSION)
        return DIMENSION;
    else
        return val;
}

int Triangle::evalFunc(int p, int q, int a, int b, int c)
{
    int e = a*p+b*q+c;
    return(e);
}

float Triangle::triArea(int xa, int ya, int xb, int yb, int xref, int yref) {
    xa -= xref;
    xb -= xref;
    ya -= yref;
    yb -= yref;
    return((float)(sqrt(pow((double)(yb*xa)-(xb*ya), (double)2))*0.5));
}

Will th ebelow code work andif not please help me fix it. Basically I am taking in a line of code and we are supposed to use fail state to figure out which kind
Will th ebelow code work andif not please help me fix it. Basically I am taking in a line of code and we are supposed to use fail state to figure out which kind
Will th ebelow code work andif not please help me fix it. Basically I am taking in a line of code and we are supposed to use fail state to figure out which kind
Will th ebelow code work andif not please help me fix it. Basically I am taking in a line of code and we are supposed to use fail state to figure out which kind
Will th ebelow code work andif not please help me fix it. Basically I am taking in a line of code and we are supposed to use fail state to figure out which kind

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site