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
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 = g.
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Will th ebelow code work andif not please help me fix it. Basically .pdf
1. 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
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)
3. 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;
4. 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;
}
7. }
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);
8. 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));
}