Skip to content

Instantly share code, notes, and snippets.

@ponkotuy
Last active July 21, 2016 13:11
Show Gist options
  • Save ponkotuy/a82489f27521fadf453018fd09998e70 to your computer and use it in GitHub Desktop.
Save ponkotuy/a82489f27521fadf453018fd09998e70 to your computer and use it in GitHub Desktop.
点を移動したりまわしたり
#include <stdio.h>
#include <math.h>
typedef struct {
double x;
double y;
} point_t;
typedef struct {
point_t* center;
double rad;
double ofs_x;
double ofs_y;
} config_t;
void to_string(char* str, const point_t* p) {
sprintf(str, "(x, y) = (%f, %f)\n", p->x, p->y);
}
point_t move(const point_t* p, double x, double y) {
point_t result = {p->x + x, p->y + y};
return result;
}
point_t rotate_center(const point_t* p, double rad) {
point_t result = {
(p->x) * cos(rad) - (p->y) * sin(rad),
(p->x) * sin(rad) + (p->y) * cos(rad)
};
return result;
}
point_t rotate(const point_t* orig, const point_t* center, double rad) {
point_t p1 = move(orig, -center->x, -center->y);
point_t p2 = rotate_center(&p1, rad);
point_t p3 = move(&p2, center->x, center->y);
return p3;
}
point_t convert_by_config(const point_t *p, const config_t* conf) {
point_t p1 = rotate(p, conf->center, conf->rad);
point_t p2 = move(&p1, conf->ofs_x, conf->ofs_y);
return p2;
}
double to_rad(double degree) {
return degree / 180.0 * M_PI;
}
int main(void) {
point_t p1 = {1.0, 2.0};
point_t p2 = move(&p1, 2.0, 1.0);
point_t p3 = rotate_center(&p2, to_rad(45.0));
config_t conf = {&p1, to_rad(45.0), 1.0, 1.0};
point_t p4 = convert_by_config(&p1, &conf);
char str[100];
to_string(str, &p2);
printf(str);
to_string(str, &p3);
printf(str);
to_string(str, &p4);
printf(str);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment