我遇到了這個項目的麻煩。我想要它畫出一個八角形,我使用的代碼對菱形和三角形等其他形狀非常合適。我不斷收到列表迭代器不可忽略,我做錯了什麼?
的octagon.cpp文件http://pastebin.com/iVfdkKEB
頭文件http://pastebin.com/a50UQi5F
它運行它所有http://pastebin.com/quepi6az
#include "shape.h"
class Octagon : public Shape
{
int radius;
void plotVertices();
public:
Octagon(Vertex point, int radius = 10);
int area();
int perimeter();
};
#include "octagon.h"
Octagon::Octagon(Vertex point, int radius) : Shape(point)
{
// constructs a Octagon of radius around a point in 2D space
if ((radius>centroid.getX()/2) || (radius>centroid.getX()/2))
{
cout << "Object must fit on screen." << endl;
system("pause");
exit(0);
this->radius = radius;
plotVertices();
}
// place your code here and add comments that describe your understanding of what is happening
}
void Octagon::plotVertices()
{
int x, y, _x, _y; // declare and intiliase variables for x and y co-ordinates
double radians;
x = centroid.getX(); // places first point A at the centroid
y = centroid.getY() + radius;
vertices.push_back(Vertex(x, y));
x = vertices.back().getX() - centroid.getX();
y = vertices.back().getY() - centroid.getY();
for (int i = 45; i < 360; i += 45) // for loop to draw the shape itself by creating the points.
// i = size of interior angle.
{
radians = i * PI/180;
_x = round(x * cos(radians) - y * sin(radians));
_y = round(y* cos(radians) + x * sin(radians));
_x = _x + centroid.getX();
_y = _y + centroid.getY();
vertices.push_back(Vertex(_x, _y));
}
}
#pragma once
#include "vertex.h"
#include "shape.h"
#include "octagon.h"
#include "console.h"
#include <iostream>
#include <list>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
list<Shape*> shapes;
int x = 20, y = 60;
shapes.push_back(new Octagon(Vertex(20, 60), 8));
list<Shape*>::iterator itr = shapes.begin();
while(itr!=shapes.end())
{
(*itr)->drawShape();
system("pause");
(*itr)->outputStatistics();
// output shape statistics
(*itr)->scale(2.0);
(*itr)->drawShape();
// scale shape (double it)
// draw shape
(*itr)->rotate(20);
(*itr)->drawShape();
// rotate shape by 20 degrees
// draw shape
itr++;
}
Console::gotoXY(0,0);
system("pause");
return 0;
}
和拉伸形狀函數
void Shape::drawShape()
{
// plots each vertex and draws a line between them using Bresenham's algorithm
// you can adjust your console font and dimensions if you want to increase the resolution of your shapes
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++;
}
drawLine(vertices.back().getX(), vertices.back().getY(), vertices.front().getX(), vertices.front().getY());
}
請張貼您的代碼文本和你的問題提供了一個[MCVE。 – NathanOliver
我看不到任何此代碼產生該錯誤的原因。請注意,你稱之爲上帝知道的一堆功能。出示您的[MCVE]。 –
真的嗎?是的,這是一個課程,我必須繪製形狀,縮放,然後旋轉它們。它對以前的形狀非常合適,但它甚至不會繪製八角形。有什麼我可能錯過了? –