C ++オブジェクト指向菱形
質問
私が課せられた課題に関する問題があると、問題は私が与えられたコードと同様に私がリソースを見つけることができないということです。私は類似点を見つけようとしているが、何も役立つものを見つけることができないので読みました。
このコードを理解しようとするのに役立つ、そしてそれを使って菱形を作成する方法。把握できない唯一のものは、形状クラスに属する菱形の形状を作成する方法です。その菱形に重心を適用してからPUSH_BACKメソッドを使用して頂点を追加します。残念ながらこのプッシュバックメソッドを使用する必要があります。私が欲しかった場所を描くための等。
私はしっかりとした週にこれを研削しますので、すばやく対応する必要があります。
//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);
};
. 解決
Rhombus
は既にShape
(class Rhombus : public Shape
)のサブクラスです。
Rhombus
のインスタンスを作成するだけです。
Shape
は、それに渡されたVertex
がpoint
インスタンス変数を自動的に初期化するために使用されるように定義されています。そのため、centroid
の場合、またはcentroid
のようなそのサブクラスの内部から、Centroid関連のデータを必要とする任意の操作のために、Shape
をCentroidとして使用できます。
同様に、List Rhombus
は、vertices
とそのすべてのサブクラスにインスタンス変数として使用可能です。あなたが他のコード(例えばShape
)を見るならば、あなたは現在の形の頂点を操作するためにShape::drawShape
がどのように使用されたかに気づくでしょう。
ここでやらなければならないことはそれにかなり似ています。たとえば、
です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));
}
.
vertices
(コンストラクタ)内にあるときは、内部登録された菱形です。 Rhombus::Rhombus
オブジェクトをもう一度作成する必要はありません。頂点を追加し、重心を定義している(すでに行われている)。
Rhombus
からRhombus
を作成していると想像してください。 4つの頂点を作成し、それらをすべての頂点のリストを追跡する構造体に追加する必要があります。