【问题标题】:C++ Object Orientated RhombusC++ 面向对象的菱形
【发布时间】:2014-07-30 15:23:48
【问题描述】:

我在分配给我的任务时遇到问题,问题是我找不到任何与我获得的代码类似的资源。我已经阅读了很多文档,试图找到相似之处,但找不到任何有用的东西。

我需要帮助来尝试理解此代码以及如何使用它来创建菱形。 我唯一不明白的是如何创建一个属于 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);
};

【问题讨论】:

  • 请把这个问题交给你的导师或助教。 Stack Overflows 格式让我们很难以确保您真正学到一些东西的方式帮助您。
  • 由单个半径值定义的菱形?那是什么样的几何图形?
  • 我问我的导师我做错了什么,和上次失败相比,我可以改进什么。他的回答是“阅读评分方案”,所以那里的帮助是不可能的。
  • 在编程方面,您需要解决此代码(以及您对基本概念的理解)的大量问题。我建议您从一个空的 main 函数开始构建您的程序,一次添加 一个 类或方法,然后再次编译所有内容并确保您没有破坏它。如果您在任何步骤中遇到困难,请就该特定问题提出问题。
  • 代码有效,我没有收到任何错误。问题是这是给出的代码。它需要使用并且需要添加到。我希望我可以开始一个全新的程序,因为根据我现在的知识,把它扔在我脸上只会令人困惑。

标签: c++ oop object shapes centroid


【解决方案1】:

如您所见,Rhombus 已经是Shape (class 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 对象。您只需通过添加顶点和定义质心(已经完成)来“装饰”实例。

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

【讨论】:

  • 如果菱形的实例被这样设置会发生什么Rhombus::Rhombus(Vertex centroid, int radius) : Shape(centroid) 它仍然是相同的还是我需要对质心做些什么? @Janaka Bandara
  • @HLG centroid 只是这里构造函数参数使用的变量名;读为centroidpoint 没有区别(Vertex 参数作为 局部变量 直接传递给 Shape 构造函数,然后将其分配给Shapecentroid 实例变量)。 (但是,在Rhombus::Rhombus 内部的vertices.push_back(...) 方法调用中,您显然必须使用centroid 而不是point。)
  • 另外,if((radius&gt;centroid.getX()/2) || ...中的centroid会引用Rhombus::Rhombus范围内的局部变量 centroid;但是,即使在这种情况下也不会产生影响,因为实例变量 centroid 已经被初始化为与局部变量相同的值。如果您想从Rhombus::Rhombus 中显式引用实例变量,您可以改用this-&gt;centroid
猜你喜欢
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2018-12-28
  • 2012-10-16
相关资源
最近更新 更多