Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "somethings.h"
- COLORREF whiteColor = RGB(255, 255, 255);
- int countOfDots = 0;
- struct sDots *dots;
- struct sDots *result;
- //HDC hdc = GetDC(FindWindowA("ConsoleWindowClass", NULL));
- void menu(){
- printf("\n\nChoose what u want \n");
- printf("Add dot [1] \n");
- printf("Delete dot [2] \n");
- printf("Show dots [3] \n");
- printf("Start algo [4] \n");
- printf("Load dots from file [5] \n");
- int choose = 0;
- scanf("%d", &choose);
- switch (choose){
- case(1) : {
- addDot();
- break;
- }
- case(2) : {
- deleteDot();
- menu();
- break;
- }
- case(3) : {
- showDots();
- menu();
- break;
- }
- case(4) : {
- startAlgo();
- break;
- }
- case(5) : {
- loadThis();
- menu();
- break;
- }
- default:{
- printf("Bad choose! \n\n");
- menu();
- }
- }
- }
- void addDot(){
- printf("x y \n");
- int tmpx = 0, tmpy = 0;
- int target = 1;
- scanf("%d %d", &tmpx, &tmpy);
- for (int i = 0; i < countOfDots; i++){
- if (tmpx == dots[i].x && tmpy == dots[i].y){
- target = 0;
- }
- }
- if (target == 0){
- printf("WRONG DOT!!\n\n");
- }
- else
- {
- dots = (struct sDots *)realloc((void *)dots, (countOfDots = countOfDots + 1)*sizeof(struct sDots));
- dots[countOfDots - 1].x = tmpx;
- dots[countOfDots - 1].y = tmpy;
- }
- menu();
- }
- void showDots(){
- printf("\n\nDots: \n");
- for (int i = 0; i < countOfDots; i++){
- printf("dot[%d] : %d %d \n", i + 1, dots[i].x, dots[i].y);
- }
- printf("\n\n");
- }
- void deleteDot(){
- showDots();
- printf("input number of dot: ");
- int numdot = 0;
- scanf("%d", &numdot);
- numdot -= 1;
- if (numdot < countOfDots){
- for (int i = numdot; i < countOfDots - 1; i++){
- dots[i].x = dots[i + 1].x;
- dots[i].y = dots[i + 1].y;
- }
- dots = (struct sDots *)realloc((void *)dots, (countOfDots = countOfDots - 1)*sizeof(struct sDots));
- }
- else{
- printf("bad input!!!!\n\n");
- }
- }
- void startAlgo(){
- int counter = 0;
- result = (struct sDots *)realloc((void *)result, (counter)*sizeof(struct sDots));
- //free(result);
- if (countOfDots < 5){
- printf("wron number of dots!\n");
- menu();
- }
- else{
- int size = 0;
- int l = 0;
- for (int i = 1; i < countOfDots; i++){
- if (dots[i].x < dots[l].x)
- l = i;
- }
- int p = l, q = 0;
- do{
- q = (p + 1) % countOfDots;
- for (int i = 0; i < countOfDots; i++)
- if (orientation(dots[p], dots[i], dots[q]) == 2)
- q = i;
- result = (struct sDots *)realloc((void *)result, (size = size + 1)*sizeof(struct sDots));
- result[size - 1].x = dots[q].x;
- result[size - 1].y = dots[q].y;
- p = q;
- } while (p != l);
- for (int i = 0; i < size; i++){
- printf("%d %d \n", result[i].x, result[i].y);
- }
- FILE *output = fopen("output.txt", "w+");
- for (int i = 0; i < size; i++){
- fprintf(output, "%d %d \n", result[i].x, result[i].y);
- }
- fclose(output);
- drawThis(size);
- }
- }
- int orientation(struct sDots a, struct sDots b, struct sDots c){
- int val = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
- if (val == 0)
- return 0;
- return (val > 0) ? 1 : 2;
- }
- void loadThis(){
- FILE *input = fopen("dots.txt", "r+");
- if (input == NULL){
- printf("Wrong file!\n\n\n\n\n");
- menu();
- }
- else{
- int counter = 0;
- while (!feof(input)){
- if (fgetc(input) == '\n')
- counter++;
- }
- fseek(input, 0, SEEK_SET);
- for (int i = 0; i < counter + 1; i++){
- dots = (struct sDots *)realloc((void *)dots, (countOfDots = countOfDots + 1)*sizeof(struct sDots));
- fscanf(input, "%d %d", &dots[countOfDots - 1].x, &dots[countOfDots - 1].y);
- }
- showDots();
- }
- }
- void drawThis(int c){
- HDC hdc = GetDC(FindWindowA("ConsoleWindowClass", NULL));
- system("cls");
- for (int i = 0; i < countOfDots; i++){
- drawDot((dots[i].x * 25) + 10, (dots[i].y * 25) + 10);
- }
- for (int i = 0; i < c - 1; i++){
- drawLine((result[i].x * 25) + 10, (result[i].y * 25) + 10, (result[i + 1].x * 25) + 10, (result[i + 1].y * 25) + 10);
- }
- drawLine((result[c - 1].x * 25) + 10, (result[c - 1].y * 25) + 10, (result[0].x * 25) + 10, (result[0].y * 25) + 10);
- }
- void drawDot(int x, int y){
- HDC hdc = GetDC(FindWindowA("ConsoleWindowClass", NULL));
- SetPixel(hdc, x, y, whiteColor);
- }
- void drawLine(int x1, int y1, int x2, int y2){
- HDC hdc = GetDC(FindWindowA("ConsoleWindowClass", NULL));
- HPEN pen = CreatePen(PS_SOLID, 1, whiteColor);
- HPEN oldpen = (HPEN)SelectObject(hdc, pen);
- MoveToEx(hdc, x1, y1, 0);
- LineTo(hdc, x2, y2);
- SelectObject(hdc, oldpen);
- DeleteObject(pen);
- }
Advertisement
Add Comment
Please, Sign In to add comment