Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "CirclePoints.h"
- int main() {
- CirclePoints circlePoints1(3, 4); //окружность. радиус равен 3, кол-во точек 4
- std::cout << "Points (1): ";
- for (auto a : circlePoints1) { //прямой итератор
- std::cout << "(" << a.x << "; " << a.y << "), "; //вывод координат точек в научной записи
- }
- std::cout << std::endl << "Reversed points (1): ";
- for (auto a = circlePoints1.end()--; a != circlePoints1.begin(); a--) { //обратный итератор
- std::cout << "(" << (*a).x << "; " << (*a).y << "), ";
- }
- CirclePoints circlePoints2(2, 8); //окружность. радиус равен 2, кол-во точек 8
- std::cout << std::endl << "Points (2): ";
- for (auto a : circlePoints2) { //прямой итератор
- std::cout << "(" << a.x << "; " << a.y << "), ";
- }
- return 0;
- }
- #include <iterator>
- #include <math.h>
- struct Point {
- double x, y;
- };
- class CirclePoints {
- private:
- double radius;
- int k; //количество точек, равномерно распределённых вдоль окружности
- public:
- CirclePoints(double radius, int k): radius(radius), k(k) {
- }
- double getRadius() {
- return radius;
- }
- void setRadius(double radius) {
- this->radius = radius;
- }
- int getK() {
- return k;
- }
- void setK(int k) {
- this->k = k;
- }
- class PointsIterator: public std::iterator<std::bidirectional_iterator_tag, int, ptrdiff_t, Point*, Point> {
- private:
- bool isDefault = true;
- CirclePoints& circlePoints;
- int currentPoint; //номер данной точки [0; k)
- void next() {
- isDefault = ++currentPoint == circlePoints.k;
- }
- void previous() {
- isDefault = --currentPoint == -1;
- }
- public:
- PointsIterator(CirclePoints& circlePoints, bool isDefault): isDefault(false), circlePoints(circlePoints) {
- currentPoint = isDefault ? circlePoints.k : -1;
- }
- bool operator == (PointsIterator other) {
- return (isDefault && other.isDefault) || (currentPoint == other.currentPoint);
- }
- bool operator != (PointsIterator other) {
- return !(*this == other);
- }
- Point operator * () {
- if (isDefault) {
- throw "not initialized iterator";
- }
- if (currentPoint == -1) {
- next();
- } else if (currentPoint == circlePoints.k) {
- previous();
- }
- double alpha = 360.0 * (double) currentPoint / (double) circlePoints.k * std::acos(-1) / 180.0; //угол относительно начальной точки (перевод в радианы)
- Point point;
- point.x = circlePoints.radius * std::cos(alpha);
- point.y = circlePoints.radius * std::sin(alpha);
- return point;
- }
- PointsIterator& operator ++ () {
- if (isDefault) {
- throw "not initialized iterator";
- }
- next();
- return *this;
- }
- PointsIterator& operator ++ (int) {
- PointsIterator& tmp = *this;
- operator++();
- return tmp;
- }
- PointsIterator& operator -- () {
- if (isDefault) {
- throw "not initialized iterator";
- }
- previous();
- return *this;
- }
- PointsIterator& operator -- (int) {
- PointsIterator& tmp = *this;
- operator--();
- return tmp;
- }
- };
- PointsIterator begin() {
- return PointsIterator(*this, false);
- }
- PointsIterator end() {
- return PointsIterator(*this, true);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement