Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GLUT/glut.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #define SIZE 200
- #define CLNUM 3
- typedef struct point{
- double x;
- double y;
- int cls;
- }Point;
- typedef enum state{
- Init,
- KS,
- MS,
- }STATE;
- Point data[SIZE];
- Point center[CLNUM];
- STATE state = Init;
- void make_data(void)
- {
- int i;
- for(i = 0; i < SIZE; i++){
- data[i].x = rand()%200;
- data[i].y = rand()%200;
- data[i].cls = -1;
- }
- for(i = 0; i < CLNUM; i++){
- center[i].x = rand()%200;
- center[i].y = rand()%200;
- center[i].cls=i;
- }
- }
- void Kstep(void)
- {
- int i,j;
- double min_range;
- for(i = 0; i < SIZE; i ++){
- min_range=4000.0; // > sqrt(x**2+y**2)
- for(j = 0; j < CLNUM; j++){
- if(min_range > sqrt(pow(data[i].x-center[j].x,2)+pow(data[i].y-center[j].y,2))){
- min_range = sqrt(pow(data[i].x-center[j].x,2)+pow(data[i].y-center[j].y,2));
- data[i].cls=j;
- }
- }
- }
- }
- void Mstep(void)
- {
- int i;
- double sum_x[CLNUM],sum_y[CLNUM];
- int count_cls[CLNUM];
- for(i = 0; i < CLNUM; i++) {
- sum_x[i] = sum_y[i] = count_cls[i] = 0;
- }
- for(i = 0; i < SIZE; i++) {
- sum_x[data[i].cls] += data[i].x;
- sum_y[data[i].cls] += data[i].y;
- count_cls[data[i].cls]++;
- }
- for(i = 0; i < CLNUM; i++) {
- center[i].x = sum_x[i] / count_cls[i];
- center[i].y = sum_y[i] / count_cls[i];
- }
- }
- void display(void)
- {
- printf("%d\n",state);
- int i;
- glClearColor(0.0, 0.0, 0.0, 0.0); // 画面のクリア色(背景色)
- glClear(GL_COLOR_BUFFER_BIT); // バッファのクリア
- switch (state) {
- case Init:
- glPointSize(5.0); // 点の太さ
- glBegin(GL_POINTS); // 点の座標を記述開始
- //glColor4f(0.7, 0.2, 0.2, 0.0); // 点の色(RGBA)
- for(i = 0; i < SIZE; i++){
- switch (data[i].cls) {
- case -1:
- glColor4f(1, 1, 1, 1);
- break;
- case 0:
- glColor4f(1, 0, 0, 1);
- break;
- case 1:
- glColor4f(0, 1, 0, 1);
- break;
- case 2:
- glColor4f(0, 0, 1, 1);
- break;
- default:
- break;
- }
- glVertex2f((data[i].x-100)/100, (data[i].y-100)/100); // 点の座標
- }
- state=KS;
- break;
- case KS:
- puts("KS");
- Kstep();
- glPointSize(5.0); // 点の太さ
- glBegin(GL_POINTS); // 点の座標を記述開始
- for(i = 0; i < SIZE; i++){
- switch (data[i].cls) {
- case -1:
- glColor4f(1, 1, 1, 1);
- break;
- case 0:
- glColor4f(1, 0, 0, 1);
- break;
- case 1:
- glColor4f(0, 1, 0, 1);
- break;
- case 2:
- glColor4f(0, 0, 1, 1);
- break;
- default:
- break;
- }
- glVertex2f((data[i].x-100)/100, (data[i].y-100)/100); // 点の座標
- }
- for(i = 0; i<CLNUM;i++){
- switch (center[i].cls) {
- case 0:
- glColor4f(1,0.5,0.5,1);
- break;
- case 1:
- glColor4f(0.5, 1, 0.5, 1);
- break;
- case 2:
- glColor4f(0.5, 0.5, 1, 1);
- break;
- default:
- break;
- }
- glVertex2f((center[i].x-100)/100, (center[i].y-100)/100);
- }
- state=MS;
- break;
- case MS:
- Mstep();
- glPointSize(5.0); // 点の太さ
- glBegin(GL_POINTS); // 点の座標を記述開始
- for(i = 0; i < SIZE; i++){
- switch (data[i].cls) {
- case -1:
- glColor4f(1, 1, 1, 1);
- break;
- case 0:
- glColor4f(1, 0, 0, 1);
- break;
- case 1:
- glColor4f(0, 1, 0, 1);
- break;
- case 2:
- glColor4f(0, 0, 1, 1);
- break;
- default:
- break;
- }
- glVertex2f((data[i].x-100)/100, (data[i].y-100)/100); // 点の座標
- }
- for(i = 0; i<CLNUM;i++){
- switch (center[i].cls) {
- case 0:
- glColor4f(1,0.5,0.5,1);
- break;
- case 1:
- glColor4f(0.5, 1, 0.5, 1);
- break;
- case 2:
- glColor4f(0.5, 0.5, 1, 1);
- break;
- default:
- break;
- }
- glVertex2f((center[i].x-100)/100, (center[i].y-100)/100);
- }
- state=KS;
- break;
- default:
- break;
- }
- glEnd(); // 座標の記述終了
- glFlush(); // コマンドの実行
- }
- void mouse(int button, int state, int x, int y)
- {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- if(state==GLUT_DOWN){
- glutPostRedisplay();
- }
- break;
- case GLUT_RIGHT_BUTTON:
- break;
- default:
- break;
- }
- }
- int main(int argc, char *argv[])
- {
- srand((unsigned)time(NULL));
- make_data();
- glutInitWindowPosition(100, 100); // 画面の出現位置
- glutInitWindowSize(400, 300); // 画面サイズ(400*300px)
- glutInit(&argc, argv); // GLUTの初期化
- glutInitDisplayMode(GLUT_RGBA); // 画面のモード(RGBA色空間を使う)
- glutCreateWindow("kmeans"); // 画面の名前
- glutDisplayFunc(display); // GLUTの再描画関数を登録
- glutMouseFunc(mouse);
- glutMainLoop(); // イベントループを繰り返し実行
- return 0;
- }
Add Comment
Please, Sign In to add comment