Skip to content

Instantly share code, notes, and snippets.

@halit
Created April 16, 2014 17:19
Show Gist options
  • Save halit/10909621 to your computer and use it in GitHub Desktop.
Save halit/10909621 to your computer and use it in GitHub Desktop.
toy coordinate system
// =====================================================================================
//
// Filename: graphic.c
//
// Description:
//
// Version: 1.0
// Created: 04/16/2014 07:30:08 PM
// Revision: none
// Compiler: gcc
//
// Author: Halit Alptekin (), [email protected]
// Organization:
//
// =====================================================================================
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct{
int x;
int y;
}Point;
typedef struct{
char** grid;
unsigned int n;
}Screen;
typedef struct{
Point** point_list;
unsigned int n;
}Shape;
Screen* init_screen(unsigned int n){
Screen* new_screen = (Screen*)calloc(1, sizeof(Screen));
assert(new_screen != NULL);
char** grid = (char**)calloc(2*n+1, sizeof(char*));
assert(grid != NULL);
int i;
for(i=0;i<2*n+1;i++) grid[i] = (char*)calloc(2*n+1, sizeof(char));
new_screen->grid = grid;
new_screen->n = n;
return new_screen;
}
Point* make_point(int x, int y){
Point* new_point = (Point*)calloc(1, sizeof(Point));
assert(new_point != NULL);
new_point->x = x;
new_point->y = y;
return new_point;
}
Shape* make_shape(Point** point_list, unsigned int n){
Shape* new_shape = (Shape*)calloc(1, sizeof(Shape));
assert(new_shape != NULL);
new_shape->point_list = point_list;
new_shape->n = n;
return new_shape;
}
Point** init_point_list(unsigned int n){
Point** new_list = (Point**)calloc(n, sizeof(Point*));
assert(new_list != NULL);
return new_list;
}
void print_shape(Screen* screen, Shape* shape){
int i, j;
for(i=0;i<shape->n;i++) screen->grid[screen->n - shape->point_list[i]->y][screen->n + shape->point_list[i]->x] = 1;
for(i=0;i<screen->n*2+1;i++){
for(j=0;j<screen->n*2+1;j++){
if(screen->grid[i][j]) putchar('.');
else putchar(' ');
}
putchar('\n');
}
}
void add_point(Point** point_list, Point* point, unsigned int i){
point_list[i] = point;
}
void clean(Screen* screen, Shape* shape){
int i;
for(i=0;i<screen->n*2+1;i++) free(screen->grid[i]);
free(screen->grid);
free(screen);
for(i=0;i<shape->n;i++) free(shape->point_list[i]);
free(shape);
}
int main(int argc, const char *argv[]){
Screen* main_screen = init_screen(4);
Point** point_list = init_point_list(17);
unsigned int i = 0;
int j;
for(j=-4;j<0;j++){
add_point(point_list, make_point(j, 0), i++); /* negative x axis */
add_point(point_list, make_point(0, j), i++); /* negative y axis */
}
add_point(point_list, make_point(0, 0), i++); /* origin */
for(j=1;j<5;j++){
add_point(point_list, make_point(j, 0), i++); /* positive x axis */
add_point(point_list, make_point(0, j), i++); /* positive y axis */
}
Shape* my_line = make_shape(point_list, 17);
print_shape(main_screen, my_line);
clean(main_screen, my_line);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment