Не сте регистриран! Регистрирайте се БЕЗПЛАТНО, за да използвате услугите на сайта!

Нова тема
Стигнах до тук
Тази тема е погледната 277 пъти
Добави темата към любими | Принтирай темата | Нова тема 
Публикувано на: 09.06.2008 16:12
simeons
Чирак

Мнения: (6)

Изскочиха ми очите вече да го гледам тоя код. Моля ако някой може да помогне. Мисля че почти е довършено вече.

#include <iostream>
#include "math.h"
#include <algorithm>
#include <vector>
using namespace std;

class Point
{
private:
    float x;
    float y;
public:
    // Contructors
    Point();
    Point(float x, float y);
    Point(const Point ©);

    // Getters and setters
    float GetX();
    void SetX(float x);
    float GetY();
    void SetY(float y);

    // Operators
    Point& operator=(const Point & b);
    bool operator==(Point b);
    bool operator<(Point b);

    // Methods - member functions
    float DistanceBetween(Point & p);

    // Destructors
    ~Point();
};


Point::Point()
{
    x = 0;
    y = 0;
}

Point::Point(float x, float y)
{
    this->x = x;
    this->y = y;
}

Point::Point(const Point & copy)
{
    this->x = copy.x;
    this->y = copy.y;
}

float Point::GetX()
{
    return x;
}

void Point::SetX(float x)
{
    this->x = x;
}

float Point::GetY()
{
    return y;
}

void Point::SetY(float y)
{
    this->y = y;
}

Point& Point::operator=(const Point & b)
{
    this->x = b.x;
    this->y = b.y;

    return *this;
}

bool Point::operator==(Point b)
{
    if (this->x == b.x && this->y == b.y)
    {
        return true;
    }
    return false;
}

bool Point::operator<(Point b)
{
    if (this->x < b.x || (this->x == b.x && this->y < b.y))
    {
        return true;
    }
    return false;
}

float Point::DistanceBetween(Point &p)
{
    return sqrt(pow(this->GetX() - p.GetX(), 2) + pow(this->GetY() - p.GetY(), 2));
}

Point::~Point()
{
    // do nothing, because there is nothing to release, no pointers or other objects inside this object
}



// 2D cross product.
// Return a positive value, if OAB makes a counter-clockwise turn,
// negative for clockwise turn, and zero if the points are collinear.
float Cross(Point &O, Point &A, Point &B)
{
    return (A.GetX() - O.GetX()) * (B.GetY() - O.GetY()) - (A.GetY() - O.GetY()) * (B.GetX() - O.GetX());
}

// Implementation of Monotone Chain Convex Hull algorithm.
// Returns a list of points on the convex hull in counter-clockwise order.
// Note: the last point in the returned list is the same as the first one.
vector<Point> ConvexHull(vector<Point> P)
{
    int n = P.size(), k = 0;

    vector<Point> H(2 * n);
 
    // Sort points lexicographically
    sort(P.begin(), P.end());
 
    // Build lower hull
    for (int i = 0; i < n; i++)
    {
        while (k >= 2 && Cross(H[k - 2], H[k - 1], P[i]) <= 0)
            k--;
        H[k++] = P[i];
    }
 
    // Build upper hull
    for (int i = n - 2, t = k + 1; i >= 0; i--)
    {
        while (k >= t && Cross(H[k - 2], H[k - 1], P[i]) <= 0)
            k--;
        H[k++] = P[i];
    }
 
    H.resize(k);
    return H;
}

void PrintPoints(vector<Point> points)
{
    for (vector<Point>::iterator iter = points.begin(); iter != points.end(); iter++)
    {
        cout << "(" << iter->GetX() << ", " << iter->GetY() << ")" << endl;
    }
}

Point Centroid(vector<Point> points)
{
    int n = points.size();
    float sumX, sumY;
    sumX = sumY = 0;

    for (vector<Point>::iterator iter = points.begin(); iter != points.end(); iter++)
    {
        sumX += iter->GetX();
        sumY += iter->GetY();
    }

    return Point(sumX / n, sumY / n);
}

float TriangleArea(Point A, Point B, Point C)
{
    float a = sqrt(pow(C.GetX() - B.GetX(), 2) + pow(C.GetY() - B.GetY(), 2));
    float b = sqrt(pow(A.GetX() - C.GetX(), 2) + pow(A.GetY() - C.GetY(), 2));
    float c = sqrt(pow(B.GetX() - A.GetX(), 2) + pow(B.GetY() - A.GetY(), 2));
    float s = (a + b + c) / 2;

    return sqrt(s * (s - a) * (s - b) * (s - c));
}

float Area(vector<Point> convexHullPoints)
{
    int n = convexHullPoints.size();
    Point centroid = Centroid(convexHullPoints);
    float area = 0;

    for (int i = 0; i < n - 1; i++)
    {
        area += TriangleArea(centroid, convexHullPoints[i], convexHullPoints[i + 1]);
    }
    return area;
}

void main()
{
    vector<Point> allPoints;
    allPoints.push_back(Point(73, 58));
    allPoints.push_back(Point(45, 19));
    allPoints.push_back(Point(89, 6));
    allPoints.push_back(Point(22, 40));
    allPoints.push_back(Point(92, 16));
    allPoints.push_back(Point(65, 66));
    allPoints.push_back(Point(55, 4));
    allPoints.push_back(Point(45, 18));
    allPoints.push_back(Point(64, 13));
    allPoints.push_back(Point(56, 81));
    allPoints.push_back(Point(73, 33));
    allPoints.push_back(Point(58, 40));
    allPoints.push_back(Point(40, 71));
    allPoints.push_back(Point(90, 60));
    allPoints.push_back(Point(16, 47));
    allPoints.push_back(Point(6, 52));
    allPoints.push_back(Point(62, 91));
    allPoints.push_back(Point(90, 83));
    allPoints.push_back(Point(69, 78));
    allPoints.push_back(Point(61, 62));
    allPoints.push_back(Point(71, 57));
    allPoints.push_back(Point(32, 53));
    allPoints.push_back(Point(67, 91));
    allPoints.push_back(Point(83, 89));
    allPoints.push_back(Point(14, 53));
    allPoints.push_back(Point(45, 35));
    allPoints.push_back(Point(11, 6));
    allPoints.push_back(Point(9, 50));
    allPoints.push_back(Point(70, 65));
    allPoints.push_back(Point(50, 47));
    allPoints.push_back(Point(65, 57));
    allPoints.push_back(Point(40, 68));
    allPoints.push_back(Point(44, 40));
    allPoints.push_back(Point(77, 61));
    allPoints.push_back(Point(27, 59));
    allPoints.push_back(Point(27, 54));
    allPoints.push_back(Point(94, 89));
    allPoints.push_back(Point(90, 88));
    allPoints.push_back(Point(61, 66));
    allPoints.push_back(Point(72, 4));
    allPoints.push_back(Point(11, 41));
    allPoints.push_back(Point(3, 74));
    allPoints.push_back(Point(44, 33));
    allPoints.push_back(Point(47, 79));
    allPoints.push_back(Point(68, 33));
    allPoints.push_back(Point(32, 64));
    allPoints.push_back(Point(66, 23));
    allPoints.push_back(Point(55, 12));
    allPoints.push_back(Point(37, 54));
    allPoints.push_back(Point(28, 95));

    vector<Point> convexHullPoints = ConvexHull(allPoints);
    PrintPoints(convexHullPoints);

    cout << "The area of the convex hull amounts: " << Area(convexHullPoints) << endl;
}


 
---------------------------
Потребител от: 14.05.08 | Всички уроци от simeons | Всички скриптове от simeons
напиши eMail напиши лично съобщение виж профила на simeons
Публикувано на: 09.06.2008 16:35
zaikoni
Самурай

Модератор

Мнения: (173)

Ами виж сега хубаво беше да занем и условието,напарвих един проект във VS 2008 тествах я и виж репорта за грешките сам говори за себе си
1>c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(195) : error C2337: 'SA_Pre' : attribute not found
1>c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(195) : error C2337: 'SA_Pre' : attribute not found
1>c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(195) : error C2337: 'SA_Pre' : attribute not found
1>c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(195) : error C2337: 'SA_Pre' : attribute not found
1>c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(195) : error C2337: 'SA_Pre' : attribute not found
1>c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(195) : fatal error C1003: error count exceeds 100; stopping compilation
1>Build log was saved at "file://c:\Documents and Settings\vanya69.ZAIKONI\My Documents\Visual Studio 2008\Projects\sega da vidim\sega da vidim\Debug\BuildLog.htm"
1>sega da vidim - 137 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Ще видя какво мога да направя да ги понамалим-пооправихме малко положението и не тръгва защото имаш функции които се използват а не са извикани


 
Това мнение е редактирано от zaikoni на 09.06.2008 16:54
---------------------------
Потребител от: 30.01.08 | Всички уроци от zaikoni | Всички скриптове от zaikoni

Човекът,който знае KAK,винаги ще има работа.Човекът който знае ЗАЩО,винаги ще бъде негов ШЕФ. "
напиши eMail напиши лично съобщение виж профила на zaikoni
Публикувано на: 09.06.2008 16:56
simeons
Чирак

Мнения: (6)

Е да май не съм съобразил за условието :)
Дадени са мновество от точки в равнината (в сорса са). Програмата трябва да пресмята следните неща:
1-Минималното разстояние между 2 точки
2-Максималното такова
3-Най-голямото лице на триегълника, който ъгли са му тия точки
4-и графика на изпъкналостта на точките [i]\ [/i]


 
---------------------------
Потребител от: 14.05.08 | Всички уроци от simeons | Всички скриптове от simeons
напиши eMail напиши лично съобщение виж профила на simeons
Публикувано на: 09.06.2008 17:04
zaikoni
Самурай

Модератор

Мнения: (173)

Ами вече не ми дава грешки ето и кода
CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <iostream>
#include "math.h"
#include <algorithm>
#include <vector>
using namespace std;

class Point
{
private:
   
float x;
   
float y;
public:
  
// Contructors
    Point
();
   
Point(float x, float y);
   
Point(float Point);

  
// Getters and setters
    float GetX
();
   
void SetX(float x);
   
float GetY();
   
void SetY(float y);

  
// Operators
    Point
& operator=(const Point & b);
   
bool operator==(Point b);
   
bool operator<(Point b);

  
// Methods - member functions
    float DistanceBetween
(Point & p);

  
// Destructors
    ~
Point();
};


Point::Point()
{
   
x = 0;
   
y = 0;
}

Point::Point(float x, float y)
{
   
this->x = x;
   
this->y = y;
}

Point::Point(float Point)
{
   
this->x = x;
   
this->y = y;
}

float Point::GetX()
{
   
return x;
}

void Point::SetX(float x)
{
   
this->x = x;
}

float Point::GetY()
{
   
return y;
}

void Point::SetY(float y)
{
   
this->y = y;
}

Point& Point::operator=(const Point & b)
{
   
this->x = b.x;
   
this->y = b.y;

   
return *this;
}

bool Point::operator==(Point b)
{
   
if (this->x == b.x && this->y == b.y)
    {
       
return true;
   
}
   
return false;
}

bool Point::operator<(Point b)
{
   
if (this->x < b.x || (this->x == b.x && this->y < b.y))
    {
       
return true;
   
}
   
return false;
}

float Point::DistanceBetween(Point &p)
{
   
return sqrt(pow(this->GetX() - p.GetX(), 2) + pow(this->GetY() - p.GetY(), 2));
}

Point::~Point()
{
  
// do nothing, because there is nothing to release, no pointers or other objects inside this object
}



//
2D cross product.
//
Return a positive value, if OAB makes a counter-clockwise turn,
//
negative for clockwise turn, and zero if the points are collinear.
float Cross(Point &O, Point &A, Point &B)
{
   
return (A.GetX() - O.GetX()) * (B.GetY() - O.GetY()) - (A.GetY() - O.GetY()) * (B.GetX() - O.GetX());
}

//
Implementation of Monotone Chain Convex Hull algorithm.
//
Returns a list of points on the convex hull in counter-clockwise order.
//
Note: the last point in the returned list is the same as the first one.
vector<Point> ConvexHull(vector<Point> P)
{
   
int n = P.size(), k = 0;

   
vector<Point> H(2 * n);
 
  
// Sort points lexicographically
   
sort(P.begin(), P.end());
 
  
// Build lower hull
   
for (int i = 0; i < n; i++)
    {
       
while (k >= 2 && Cross(H[k - 2], H[k - 1], P[i]) <= 0)
           
k--;
       
H[k++] = P[i];
   
}
 
  
// Build upper hull
   
for (int i = n - 2, t = k + 1; i >= 0; i--)
    {
       
while (k >= t && Cross(H[k - 2], H[k - 1], P[i]) <= 0)
           
k--;
       
H[k++] = P[i];
   
}
 
   
H.resize(k);
   
return H;
}

void PrintPoints(vector<Point> points)
{
   
for (vector<Point>::iterator iter = points.begin(); iter != points.end(); iter++)
    {
       
cout << "(" << iter->GetX() << ", " << iter->GetY() << ")" << endl;
   
}
}

Point Centroid(vector<Point> points)
{
   
int n = points.size();
   
float sumX, sumY;
   
sumX = sumY = 0;

   
for (vector<Point>::iterator iter = points.begin(); iter != points.end(); iter++)
    {
       
sumX += iter->GetX();
       
sumY += iter->GetY();
   
}

   
return Point(sumX / n, sumY / n);
}

float TriangleArea(Point A, Point B, Point C)
{
   
float a = sqrt(pow(C.GetX() - B.GetX(), 2) + pow(C.GetY() - B.GetY(), 2));
   
float b = sqrt(pow(A.GetX() - C.GetX(), 2) + pow(A.GetY() - C.GetY(), 2));
   
float c = sqrt(pow(B.GetX() - A.GetX(), 2) + pow(B.GetY() - A.GetY(), 2));
   
float s = (a + b + c) / 2;

    return sqrt(s * (s - a) * (s - b) * (s - c));
}

float Area(vector<Point>  convexHullPoints)
{
    int n = convexHullPoints.size();
    Point centroid = Centroid(convexHullPoints);
    float area = 0;

    for (int i = 0; i < n - 1; i++)
    {
        area += TriangleArea(centroid, convexHullPoints[i], convexHullPoints[i + 1]);
    }
    return area;
}

void main()
{
    vector<Point> allPoints;
    allPoints.push_back(Point(73, 58));
    allPoints.push_back(Point(45, 19));
    allPoints.push_back(Point(89, 6));
    allPoints.push_back(Point(22, 40));
    allPoints.push_back(Point(92, 16));
    allPoints.push_back(Point(65, 66));
    allPoints.push_back(Point(55, 4));
    allPoints.push_back(Point(45, 18));
    allPoints.push_back(Point(64, 13));
    allPoints.push_back(Point(56, 81));
    allPoints.push_back(Point(73, 33));
    allPoints.push_back(Point(58, 40));
    allPoints.push_back(Point(40, 71));
    allPoints.push_back(Point(90, 60));
    allPoints.push_back(Point(16, 47));
    allPoints.push_back(Point(6, 52));
    allPoints.push_back(Point(62, 91));
    allPoints.push_back(Point(90, 83));
    allPoints.push_back(Point(69, 78));
    allPoints.push_back(Point(61, 62));
    allPoints.push_back(Point(71, 57));
    allPoints.push_back(Point(32, 53));
    allPoints.push_back(Point(67, 91));
    allPoints.push_back(Point(83, 89));
    allPoints.push_back(Point(14, 53));
    allPoints.push_back(Point(45, 35));
    allPoints.push_back(Point(11, 6));
    allPoints.push_back(Point(9, 50));
    allPoints.push_back(Point(70, 65));
    allPoints.push_back(Point(50, 47));
    allPoints.push_back(Point(65, 57));
    allPoints.push_back(Point(40, 68));
    allPoints.push_back(Point(44, 40));
    allPoints.push_back(Point(77, 61));
    allPoints.push_back(Point(27, 59));
    allPoints.push_back(Point(27, 54));
    allPoints.push_back(Point(94, 89));
    allPoints.push_back(Point(90, 88));
    allPoints.push_back(Point(61, 66));
    allPoints.push_back(Point(72, 4));
    allPoints.push_back(Point(11, 41));
    allPoints.push_back(Point(3, 74));
    allPoints.push_back(Point(44, 33));
    allPoints.push_back(Point(47, 79));
    allPoints.push_back(Point(68, 33));
    allPoints.push_back(Point(32, 64));
    allPoints.push_back(Point(66, 23));
    allPoints.push_back(Point(55, 12));
    allPoints.push_back(Point(37, 54));
    allPoints.push_back(Point(28, 95));

    vector<Point> convexHullPoints = ConvexHull(allPoints);
    PrintPoints(convexHullPoints);

    cout << "The area of the convex hull amounts: " << Area(convexHullPoints) << endl;
}


 


 
Това мнение е редактирано от zaikoni на 09.06.2008 17:04
---------------------------
Потребител от: 30.01.08 | Всички уроци от zaikoni | Всички скриптове от zaikoni

Човекът,който знае KAK,винаги ще има работа.Човекът който знае ЗАЩО,винаги ще бъде негов ШЕФ. "
напиши eMail напиши лично съобщение виж профила на zaikoni
Публикувано на: 10.06.2008 00:48
gomaker
Калфа

Мнения: (28)

Абе, няма лошо. Класове, вектори, итератори, предефиниране на оператори - нали трябва да е C++.
Използваме перални за пране на чорапи. Може би с учебна цел. Само като видях инициализацията на точките ми прилоша.

А може далеч по-просто да е - без четвъртото условие:
http://it-place.net/forum/2/C__C++__.NET/13752/C%2B%2B+%3F%3F%3F


 
---------------------------
Потребител от: 15.01.08 | Всички уроци от gomaker | Всички скриптове от gomaker
напиши eMail напиши лично съобщение виж профила на gomaker
 1 посетител чете тази тема (0 потребители и 1 гост)  
Активни потребители: ---
   





IT-PLACE.NET © 2004 - 2008