C ++面向对象的菱形

时间:2014-07-30 15:23:49

标签: c++ oop object shapes centroid

我在分配问题时遇到了问题,问题是我无法找到任何类似于我已经给出的代码的资源。我读了很多文件,试图找到相似之处但找不到任何有用的东西。

我需要帮助尝试理解此代码以及如何使用它来创建Rhombus。 我唯一能掌握的是如何创建属于Shape类的菱形形状。将质心应用于菱形,然后使用push_back方法添加顶点。不幸的是需要使用这种推回方法,我只使用drawLine(10,10,40,10)检查失败;用于绘制我想要的线条。

我要坚持这个坚实的一周,所以我应该快速回应。

//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);
};

1 个答案:

答案 0 :(得分:0)

正如您所看到的,Rhombus已经是Shapeclass Rhombus : public Shape)的子类,因此您必须创建Rhombus的实例才能实现所有魔法

Shape的定义使得传递给它的Vertex(作为point参数)用于自动初始化centroid实例变量;因此,您可以使用centroid作为质心,用于需要与质心相关的数据的任何操作,可以来自Shape内,也可以来自其中一个子类,如Rhombus

类似地,列表vertices可用于Shape及其所有子类作为实例变量。如果您查看其余代码(例如Shape::drawShape),您会注意到vertices如何用于操作当前形状的顶点。

你在这里要做的非常相似。例如,

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));
}

当你在Rhombus::Rhombus(构造函数)中时,你已经在里面刚刚创建的菱形;您不必再次创建Rhombus对象。你只需要装饰&#34;实例通过添加顶点和定义质心(已经完成)。

想象一下,您正在Rhombus中创建Shape;你需要创建4个顶点并将它们添加到跟踪所有顶点列表的结构中。