Question

I'm having issues with an assignment I've been given, the problem is that I cannot find any resources in anyway similar to the code I've been given. I've read through so many documents trying to find similarities but cannot find anything useful.

I need help trying to understand this code and how to use it to create a Rhombus. The only things I can't grasp is how to create a Rhombus shape that belongs to the Shape class. Apply a centroid to that Rhombus and then add vertices using a push_back method. Unfortunately this push back method needs to be used, I failed the exam by just using drawLine(10,10,40,10); etc for drawing lines where I wanted.

I'll be grinding this for a solid week so I should respond quickly.

//This is the rhombus.cpp file
#include "rhombus.h"

Rhombus::Rhombus(Vertex point, int radius) : Shape(point)
{
    if((radius>centroid.getX()/2) || (radius>centroid.getY()/2)) // Inteded to be a y?
    {
        cout << "Object must fit on screen." << endl;
        system("pause");
        exit(0);
    }

    Rhombus shape1(20, 20);
    shape1.plotVertices();

}

void Rhombus::plotVertices()
{
    //vertices.push_back(Vertex(centroid.getX(), centroid.getY() + radius));
    //vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
    //vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
    //vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
}

// This is the rhombus.h file
#include "shape.h"

class Rhombus : public Shape 
{
    int radius;
    void plotVertices();
    Rhombus(Vertex point, int radius = 10);
    int area();
    int perimeter();
};

// This is the shape.cpp file
#include "shape.h"

Shape::Shape(Vertex point) : centroid(point)
{
    // constructs a shape

}

void Shape::drawShape()
{

    list<Vertex>::iterator current = vertices.begin();
    list<Vertex>::iterator previous = vertices.begin();
    while(current!=vertices.end())
    {
        Console::gotoXY((*current).getX(),(*current).getY());
        cout << "*";
        if(current!=vertices.begin())
            drawLine((*current).getX(),(*current).getY(), (*previous).getX(),            (*previous).getY());
        previous = current;
        current++;
    }
    previous = vertices.begin();

    //Debug assertion error here.
    drawLine(vertices.back().getX(), vertices.back().getY(), vertices.front().getX(),     vertices.front().getY());
}

void Shape::drawLine(int x1, int y1, int x2, int y2)
{      

    bool steep = (abs(y2 - y1) > abs(x2 - x1));
    if(steep)
    {
        swap(x1, y1);
        swap(x2, y2);
    }

    if(x1 > x2)
    {
        swap(x1, x2);
        swap(y1, y2);
    }

    int dx = x2 - x1;
    int dy = abs(y2 - y1);

    float error = dx / 2.0f;
    int ystep = (y1 < y2) ? 1 : -1;
    int y = y1;
    int maxX = x2;

    for(int x=x1; x<maxX; x++)
    {
        if(steep)
        {
            Console::gotoXY(y,x);
            cout << "*";
        }
        else
        {
            Console::gotoXY(x,y);
        cout << "*";
        }
        error -= dy;
        if(error < 0)
        {
            y += ystep;
            error += dx;
        }
    }
}


double Shape::round(double x)
{
    if (ceil(x+0.5) == floor(x+0.5))
    {
        int a = (int) ceil(x);
        if (a%2 == 0)
            return ceil(x);
        else
            return floor(x);
    }
    else 
        return floor(x+0.5);
}

void Shape::outputStatistics()
{

}

// This is the shape.h file
#pragma once
#include "console.h"
#include "vertex.h"
#include <iostream>
#include <list>
#include <cstdlib>
#include <cmath>
using namespace std;

#define PI 3.14159265358979323846

class Shape
{
    list<Vertex>::iterator itr;
protected:
    list<Vertex> vertices;
    Vertex centroid;
    void drawLine(int x1, int y1, int x2, int y2);

    Shape(Vertex point);
    double round(double x);

public:
    void drawShape();
    virtual int area() = 0;
    virtual int perimeter() = 0;
    virtual void outputStatistics();
    void rotate(double degrees);
    void scale(double factor);
};
Was it helpful?

Solution

As you can see, Rhombus is already a subclass of Shape (class Rhombus : public Shape) so you just have to create an instance of Rhombus for all the magic to happen.

Shape is defined such that the Vertex passed to it (as the point parameter) is used to initialize the centroid instance variable automatically; so you can use centroid as the centroid for any operation requiring centroid-related data, either from within Shape or from within one of its subclasses like Rhombus.

Similarly, the list vertices is available for Shape and all its subclasses as an instance variable. If you look at the rest of the code (e.g. Shape::drawShape), you will notice how vertices has been used for manipulating the vertices of the current shape.

What you have to do here is quite similar to that. For example,

Rhombus::Rhombus(Vertex point, int radius) : Shape(point)
{
    if((radius>centroid.getX()/2) || (radius>centroid.getY()/2)) // Inteded to be a y?
    {
        cout << "Object must fit on screen." << endl;
        system("pause");
        exit(0);
    }

    // create vertices for a rhombus with horizontal and vertical diagonals
    vertices.push_back(Vertex(point.getX() - radius, point.getY()));
    vertices.push_back(Vertex(point.getX(), point.getY() - radius));
    vertices.push_back(Vertex(point.getX() + radius, point.getY()));
    vertices.push_back(Vertex(point.getX(), point.getY() + radius));
}

When you are inside Rhombus::Rhombus (the constructor), you are already inside a rhombus that has just been created; you don't have to create a Rhombus object again. You just have to "decorate" the instance by adding vertices, and defining a centroid (which has been already done).

Just imagine that you are creating a Rhombus out of a Shape; you need to create 4 vertices and add them to the structure that keeps track of the list of all vertices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top