Guest User

Untitled

a guest
Dec 16th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #include <GLUT/glut.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5.  
  6. #define SIZE 200
  7. #define CLNUM 3
  8.  
  9. typedef struct point{
  10. double x;
  11. double y;
  12. int cls;
  13. }Point;
  14. typedef enum state{
  15. Init,
  16. KS,
  17. MS,
  18. }STATE;
  19.  
  20. Point data[SIZE];
  21. Point center[CLNUM];
  22. STATE state = Init;
  23.  
  24. void make_data(void)
  25. {
  26. int i;
  27. for(i = 0; i < SIZE; i++){
  28. data[i].x = rand()%200;
  29. data[i].y = rand()%200;
  30. data[i].cls = -1;
  31. }
  32.  
  33. for(i = 0; i < CLNUM; i++){
  34. center[i].x = rand()%200;
  35. center[i].y = rand()%200;
  36. center[i].cls=i;
  37. }
  38.  
  39. }
  40.  
  41. void Kstep(void)
  42. {
  43. int i,j;
  44. double min_range;
  45. for(i = 0; i < SIZE; i ++){
  46. min_range=4000.0; // > sqrt(x**2+y**2)
  47. for(j = 0; j < CLNUM; j++){
  48. if(min_range > sqrt(pow(data[i].x-center[j].x,2)+pow(data[i].y-center[j].y,2))){
  49. min_range = sqrt(pow(data[i].x-center[j].x,2)+pow(data[i].y-center[j].y,2));
  50. data[i].cls=j;
  51. }
  52. }
  53. }
  54. }
  55. void Mstep(void)
  56. {
  57. int i;
  58. double sum_x[CLNUM],sum_y[CLNUM];
  59. int count_cls[CLNUM];
  60. for(i = 0; i < CLNUM; i++) {
  61. sum_x[i] = sum_y[i] = count_cls[i] = 0;
  62. }
  63.  
  64. for(i = 0; i < SIZE; i++) {
  65. sum_x[data[i].cls] += data[i].x;
  66. sum_y[data[i].cls] += data[i].y;
  67. count_cls[data[i].cls]++;
  68. }
  69.  
  70. for(i = 0; i < CLNUM; i++) {
  71. center[i].x = sum_x[i] / count_cls[i];
  72. center[i].y = sum_y[i] / count_cls[i];
  73. }
  74. }
  75.  
  76. void display(void)
  77. {
  78. printf("%d\n",state);
  79. int i;
  80. glClearColor(0.0, 0.0, 0.0, 0.0); // 画面のクリア色(背景色)
  81. glClear(GL_COLOR_BUFFER_BIT); // バッファのクリア
  82. switch (state) {
  83. case Init:
  84. glPointSize(5.0); // 点の太さ
  85. glBegin(GL_POINTS); // 点の座標を記述開始
  86. //glColor4f(0.7, 0.2, 0.2, 0.0); // 点の色(RGBA)
  87. for(i = 0; i < SIZE; i++){
  88. switch (data[i].cls) {
  89. case -1:
  90. glColor4f(1, 1, 1, 1);
  91. break;
  92. case 0:
  93. glColor4f(1, 0, 0, 1);
  94. break;
  95. case 1:
  96. glColor4f(0, 1, 0, 1);
  97. break;
  98. case 2:
  99. glColor4f(0, 0, 1, 1);
  100. break;
  101. default:
  102. break;
  103. }
  104. glVertex2f((data[i].x-100)/100, (data[i].y-100)/100); // 点の座標
  105. }
  106. state=KS;
  107. break;
  108. case KS:
  109. puts("KS");
  110. Kstep();
  111. glPointSize(5.0); // 点の太さ
  112. glBegin(GL_POINTS); // 点の座標を記述開始
  113. for(i = 0; i < SIZE; i++){
  114. switch (data[i].cls) {
  115. case -1:
  116. glColor4f(1, 1, 1, 1);
  117. break;
  118. case 0:
  119. glColor4f(1, 0, 0, 1);
  120. break;
  121. case 1:
  122. glColor4f(0, 1, 0, 1);
  123. break;
  124. case 2:
  125. glColor4f(0, 0, 1, 1);
  126. break;
  127. default:
  128. break;
  129. }
  130. glVertex2f((data[i].x-100)/100, (data[i].y-100)/100); // 点の座標
  131. }
  132. for(i = 0; i<CLNUM;i++){
  133. switch (center[i].cls) {
  134. case 0:
  135. glColor4f(1,0.5,0.5,1);
  136. break;
  137. case 1:
  138. glColor4f(0.5, 1, 0.5, 1);
  139. break;
  140. case 2:
  141. glColor4f(0.5, 0.5, 1, 1);
  142. break;
  143. default:
  144. break;
  145. }
  146. glVertex2f((center[i].x-100)/100, (center[i].y-100)/100);
  147. }
  148. state=MS;
  149. break;
  150. case MS:
  151. Mstep();
  152. glPointSize(5.0); // 点の太さ
  153. glBegin(GL_POINTS); // 点の座標を記述開始
  154. for(i = 0; i < SIZE; i++){
  155. switch (data[i].cls) {
  156. case -1:
  157. glColor4f(1, 1, 1, 1);
  158. break;
  159. case 0:
  160. glColor4f(1, 0, 0, 1);
  161. break;
  162. case 1:
  163. glColor4f(0, 1, 0, 1);
  164. break;
  165. case 2:
  166. glColor4f(0, 0, 1, 1);
  167. break;
  168. default:
  169. break;
  170. }
  171. glVertex2f((data[i].x-100)/100, (data[i].y-100)/100); // 点の座標
  172. }
  173. for(i = 0; i<CLNUM;i++){
  174. switch (center[i].cls) {
  175. case 0:
  176. glColor4f(1,0.5,0.5,1);
  177. break;
  178. case 1:
  179. glColor4f(0.5, 1, 0.5, 1);
  180. break;
  181. case 2:
  182. glColor4f(0.5, 0.5, 1, 1);
  183. break;
  184. default:
  185. break;
  186. }
  187. glVertex2f((center[i].x-100)/100, (center[i].y-100)/100);
  188. }
  189. state=KS;
  190. break;
  191. default:
  192. break;
  193. }
  194.  
  195.  
  196. glEnd(); // 座標の記述終了
  197. glFlush(); // コマンドの実行
  198. }
  199.  
  200. void mouse(int button, int state, int x, int y)
  201. {
  202. switch (button) {
  203. case GLUT_LEFT_BUTTON:
  204. if(state==GLUT_DOWN){
  205. glutPostRedisplay();
  206. }
  207. break;
  208. case GLUT_RIGHT_BUTTON:
  209. break;
  210. default:
  211. break;
  212. }
  213. }
  214.  
  215. int main(int argc, char *argv[])
  216. {
  217. srand((unsigned)time(NULL));
  218. make_data();
  219. glutInitWindowPosition(100, 100); // 画面の出現位置
  220. glutInitWindowSize(400, 300); // 画面サイズ(400*300px)
  221. glutInit(&argc, argv); // GLUTの初期化
  222. glutInitDisplayMode(GLUT_RGBA); // 画面のモード(RGBA色空間を使う)
  223. glutCreateWindow("kmeans"); // 画面の名前
  224. glutDisplayFunc(display); // GLUTの再描画関数を登録
  225. glutMouseFunc(mouse);
  226. glutMainLoop(); // イベントループを繰り返し実行
  227. return 0;
  228. }
Add Comment
Please, Sign In to add comment