Commit 4ddf4daa authored by xingqiao's avatar xingqiao
Browse files

helloworld basic code files

parent e4be3ef0
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#define M_PI 3.14159265358979323846
typedef struct polygon {
......@@ -11,20 +12,91 @@ typedef struct polygon {
bool contains(polygon_t *pg, double x, double y) {
// point is contained if it is on the "inside" of the four lines
double x1 = 0;
double x2 = 0;
double y1 = 0;
double y2 = 0;
double crosslnit = 0;
bool assigned = false;
for (int a = 0;a < pg->n_points;a++) {
if (a == pg->n_points-1) {
x2 = pg->xs[0];
y2 = pg->ys[0];
}
else {
x2 = pg->xs[a+1];
y2 = pg->ys[a+1];
}
x1 = pg->xs[a];
y1 = pg->ys[a];
double vector1[2] = {x - x2, y - y2};
double vector2[2] = {x1 - x2,y1 - y2};
if(!assigned) {
crosslnit = vector1[0]*vector2[1]-vector1[1]*vector2[0];
assigned = true;
}
double cross = vector1[0]*vector2[1]-vector1[1]*vector2[0];
if (cross*crosslnit < 0) {
return false;
}
}
return true;
}
bool lines_intersect(double x0, double y0, double x1, double y1,
double x2, double y2, double x3, double y3) {
double vector1[2] = {x1 - x0,y1 - y0};
double vector2[2] = {x2 - x0,y2 - y0};
double vector3[2] = {x3 - x0,y3 - y0};
double cross1 = vector1[0]*vector2[1]-vector1[1]*vector2[0];
double cross2 = vector1[0]*vector3[1]-vector1[1]*vector3[0];
double mul1 = cross1 * cross2;
double vector4[2] = {x3 - x2, y3 - y2};
double vector5[2] = {x1 - x2, y1 - y2};
double vector6[2] = {x0 - x2, y0 - y2};
double cross3 = vector4[0]*vector5[1] - vector4[1]*vector5[0]; double cross4 = vector4[0]*vector6[1] - vector4[1]*vector6[0];
double mul2 = cross3 * cross4;
bool result = (mul1 <=0 && mul2 <= 0) && !(mul1 ==0 && mul2 == 0);
if (!result) {
printf("NO line intersection between (%.5f, %.5f), (%.5f, %.5f) and (%.5f, %.5f), (%.5f, %.5f)\n", x0, y0, x1, y1, x2, y2, x3, y3);
} else {
printf("line intersection between (%.5f, %.5f), (%.5f, %.5f) and (%.5f, %.5f), (%.5f, %.5f)\n", x0, y0, x1, y1, x2, y2, x3, y3);
}
return result;
}
bool check_intersection(polygon_t *pg1, polygon_t *pg2) {
bool any_intersection = false;
double x0 = 0, y0 = 0, x1 = 0, y1 = 0;
double x2 = 0, y2 = 0, x3 = 0, y3 = 0;
for(int i = 0; i < pg1->n_points; i++) {
double x0 = pg1->xs[i], y0 = pg1->ys[i];
if(i == pg1->n_points - 1) {
x1 = pg1->xs[0];
y1 = pg1->ys[0];
} else {
x1 = pg1->xs[i + 1];
y1 = pg1->ys[i + 1];
}
for(int j = 0; j < pg2->n_points; j++) {
double x2 = pg2->xs[j], y2 = pg2->ys[j];
if(j == pg2->n_points -1) {
x3 = pg2->xs[0];
y3 = pg2->ys[0];
} else {
x3 = pg2->xs[j + 1];
y3 = pg2->ys[j + 1];
}
if(lines_intersect(x0,y0,x1,y1,x2,y2,x3,y3)) {
any_intersection = true;
}
}
}
return any_intersection;
}
......@@ -33,43 +105,104 @@ bool check_collides(polygon_t *pg1, polygon_t *pg2) {
bool contains1 = false;
for (int i = 0; i < pg2->n_points; i++) {
if (contains(pg1, pg2->xs[i], pg2->ys[i])) {
contains1 = true;
}
}
if(contains1) {
printf("polygon1 contains polygon2\n");
}
bool contains2 = false;
for (int i = 0; i < pg1->n_points; i++) {
if (contains(pg2, pg1->xs[i], pg1->ys[i])) {
contains2 = true;
}
}
if(contains2) {
printf("polygon2 contains polygon1\n");
}
return intersects || contains1 || contains2;
}
polygon_t parse_polygon(FILE *f) {
polygon_t pg = {0};
double shift_X = 0, shift_Y = 0, rot = 0;
fscanf(f,"%lf %lf %lf", &shift_X, &shift_Y, &rot);
// printf("shift_x: %lf, shift_y: %lf, rot: %lf\n", shift_X, shift_Y, rot);
fscanf(f,"%d", &pg.n_points);
if(pg.n_points > 16){
fprintf(stderr,"points greater than 16\n");
exit(1);
}
int checker = 0;
for(int i = 0; i < pg.n_points; i++) {
checker += fscanf(f,"%lf", &pg.xs[i]);
}
for(int j = 0; j < pg.n_points; j++) {
checker += fscanf(f,"%lf", &pg.ys[j]);
}
if(checker != 2*pg.n_points) {
fprintf(stderr,"a polygons.csv number is invalid\n");
exit(1);
}
for(int i = 0; i < pg.n_points; i++) {
double new_X = pg.xs[i], new_Y = pg.ys[i];
double ang = rot*M_PI/180;
new_X = pg.xs[i]*cos(ang) - pg.ys[i]*sin(ang);
new_Y = pg.xs[i]*sin(ang) + pg.ys[i]*cos(ang);
new_X += shift_X;
new_Y += shift_Y;
pg.xs[i] = new_X;
pg.ys[i] = new_Y;
// printf("x[%d]: %lf\n", i, pg.xs[i]);
// printf("n_x[%d]: %lf\n", i, new_X);
}
return pg;
}
int main(void) {
FILE *f = fopen("polygons.csv", "r");
if (!f) {
fprintf(stderr, "polygons.csv is missing\n");
return 1;
}
char throwaway[256];
fgets(throwaway, sizeof(throwaway), f);
polygon_t pg1 = parse_polygon(f);
polygon_t pg2 = parse_polygon(f);
if(pg1.n_points != pg2.n_points) {
fprintf(stderr,"a polygons.csv number is invalid\n");
return 1;
}
for(int i = 0; i < pg1.n_points; i++) {
if(i == pg1.n_points - 1){
printf("x%d %.5f y%d %.5f", i, pg1.xs[i], i, pg1.ys[i]);
} else{
printf("x%d %.5f y%d %.5f ", i, pg1.xs[i], i, pg1.ys[i]);
}
}
printf("\n");
for(int i = 0; i < pg2.n_points; i++) {
if(i == pg1.n_points - 1){
printf("x%d %.5f y%d %.5f", i, pg2.xs[i], i, pg2.ys[i]);
} else{
printf("x%d %.5f y%d %.5f ", i, pg2.xs[i], i, pg2.ys[i]);
}
}
printf("\n");
fclose(f);
if (check_collides(, )) {
if (check_collides(&pg1,&pg2)) {
printf("collision!\n");
} else {
printf("no collision\n");
}
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment