1/* 2 * Copyright 2007, Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stephan A��mus <superstippi@gmx.de> 7 */ 8#include "Polygon.h" 9 10#include <math.h> 11#include <stdio.h> 12#include <stdlib.h> 13 14// constructor 15Polygon::Polygon(BRect bounds, int32 vertices) 16 : fPoints(vertices), 17 fBounds(bounds) 18{ 19 float min = bounds.Width() / 64000.0; 20 float max = bounds.Width() / 320.0; 21 for (int32 i = 0; i < vertices; i++) { 22 point_vector* pv = new point_vector; 23 pv->point.x = bounds.left + fmod(lrand48(), bounds.Width()); 24 pv->point.y = bounds.top + fmod(lrand48(), bounds.Height()); 25 pv->vector.x = min + fmod(lrand48(), max - min); 26 pv->vector.y = min + fmod(lrand48(), max - min); 27 fPoints.AddItem((void*)pv); 28 } 29} 30 31// constructor 32Polygon::Polygon(BRect bounds, BList points) 33 : fPoints(points.CountItems()), 34 fBounds(bounds) 35{ 36 fPoints = points; 37} 38 39// destructor 40Polygon::~Polygon() 41{ 42 while (point_vector* pv = (point_vector*)fPoints.RemoveItem((int32)0)) 43 delete pv; 44} 45 46// Step 47Polygon* 48Polygon::Step() const 49{ 50 BList points(CountPoints()); 51 for (int32 i = 0; point_vector *pv = (point_vector*)fPoints.ItemAt(i); i++) { 52 point_vector* npv = new point_vector; 53 BPoint p = pv->point + pv->vector; 54 if (p.x < fBounds.left || p.x > fBounds.right) 55 npv->vector.x = -pv->vector.x; 56 else 57 npv->vector.x = pv->vector.x; 58 if (p.y < fBounds.top || p.y > fBounds.bottom) 59 npv->vector.y = -pv->vector.y; 60 else 61 npv->vector.y = pv->vector.y; 62 npv->point = pv->point + npv->vector; 63 points.AddItem((void*)npv); 64 } 65 return new Polygon(fBounds, points); 66} 67 68// CountPoints 69uint32 70Polygon::CountPoints() const 71{ 72 return fPoints.CountItems(); 73} 74 75// PointAt 76BPoint 77Polygon::PointAt(int32 index) const 78{ 79 BPoint p; 80 if (point_vector* pv = (point_vector*)fPoints.ItemAt(index)) 81 p = pv->point; 82 return p; 83} 84