1

I am trying to draw a minimum enclosing circle and I need spline curves for points. but also I shouldn't use libraries. For preview, I used lines but they need curves. How can I fix it?

Which one is true arc, line or points?

#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <conio.h>

struct nokta
{
    int x,y;
};
//-------------------------------------------------------------//

int main()
{
    int gd = DETECT;
    int gm;
    initgraph(&gd, &gm, "C:\\TC\\BGI");
    initwindow(800,800);
    struct nokta noktalar[50];
    FILE *noktaal=fopen("noktalar.txt","r");
    int i=0;
    int sayac=0;
    if(noktaal==NULL)
    {
        printf("Dosya yok");
    }
    while(!feof(noktaal))
    {

        fscanf(noktaal,"%d %d",&noktalar[i].x,&noktalar[i].y);
        sayac++;
        i++;
    };

    for(int i=0; i<sayac-1; i++)
    {
        printf("%d %d \n",noktalar[i].x,noktalar[i].y);
    }
    fclose(noktaal);

//----------------------------------------------------------------//
    if(sayac==1)//If there is no point
    {
        printf("Lutfen koordinat giriniz.");
    }

    if(sayac==2)// If there is one point
    {
        printf("Lutfen birden fazla koordinat giriniz.");
    }

    if(sayac==3)//If there is two point
    {
        int uzunluk1=abs(noktalar[1].x-noktalar[0].x);
        int uzunluk2=abs(noktalar[1].y-noktalar[0].y);
        double yaricap=sqrt(pow(uzunluk1,2)+pow(uzunluk2,2));
        float merkezx=(noktalar[1].x+noktalar[0].x)/2.0;
        float merkezy=(noktalar[1].y+noktalar[0].y)/2.0;
        printf("Yaricap:%.2f\n",yaricap/2);
        printf("Merkez noktasi:{%.2f,%.2f}",merkezx,merkezy);
        line(getmaxx()/2,0,getmaxx()/2,getmaxy());//y düzlemi
        line(0,getmaxy()/2,getmaxx(),getmaxy()/2);//x düzlemi
        putpixel(((getmaxx()/2)+(noktalar[0].x*20)),(getmaxy()/2)-(noktalar[0].y*20),GREEN);
        putpixel(((getmaxx()/2)+(noktalar[1].x*20)),(getmaxy()/2)-(noktalar[1].y*20),GREEN);
        for(int i=20; i<=800; i=i+20)
        {
            line(i,getmaxy()/2.03,i,getmaxy()/1.97);
        }
        for(int i=20; i<=800; i=i+20)
        {
            line(getmaxx()/2.03,i,getmaxx()/1.97,i);
        }
        circle((getmaxx()/2)+(merkezx*20),(getmaxy()/2)-(merkezy*20),yaricap*7.9);
        getch();
        closegraph();
    }

    if(sayac>3)//If there is three or more points.
    {
        int karetoplam;
        double enbuyuk=1.0,yaricap1;
        float merkez1x,merkez1y;
        int sayac1=0;
        int ikinoktauzakligi=0;
        for(int i=0; i<sayac-1; i++)
        {
            for(int j=0; j<sayac-1; j++)
            {
                float ykare=pow(abs(noktalar[i].y-noktalar[j].y),2);
                float xkare=pow(abs(noktalar[i].x-noktalar[j].x),2);
                karetoplam=ykare+xkare;
                if(enbuyuk<=karetoplam)
                {
                    enbuyuk=karetoplam;
                }
                if(enbuyuk==karetoplam)
                {
                    merkez1x=(noktalar[i].x+noktalar[j].x)/2.0;
                    merkez1y=(noktalar[i].y+noktalar[j].y)/2.0;
                    ++sayac1;
                }
            }
        }

        if(sayac1>=2)
        {
            double cap1=sqrt(enbuyuk);
            yaricap1=cap1/2;
            printf("Ilk yari capi:%.2f\n",yaricap1);
            printf("Ilk merkez noktasi:{%.2f,%.2f}\n",merkez1x,merkez1y);
            line(getmaxx()/2,0,getmaxx()/2,getmaxy());//y düzlemi
            line(0,getmaxy()/2,getmaxx(),getmaxy()/2);//x düzlemi
            for(int i=20; i<=800; i=i+20)
            {
                line(i,getmaxy()/2.03,i,getmaxy()/1.97);
            }
            for(int i=20; i<=800; i=i+20)
            {
                line(getmaxx()/2.03,i,getmaxx()/1.97,i);
            }
            for(int i=0; i<sayac-1; i++)
            {
                for(int j=0; j<sayac-1; j++)
                {
                    putpixel((getmaxx()/2)+(noktalar[i].x*20),(getmaxy()/2)-(noktalar[i].y*20),GREEN);
                }
            }
            float uzunluk2;
            float arttir=0.0;
            float enbuyuk2=0.0;
            for(int i=0; i<100;i++)
            {
            for(int j=0; j<sayac-1; j++)
            {
                uzunluk2=sqrt((pow(noktalar[j].x,2)+pow(noktalar[j].y,2))-(pow(merkez1x,2)+pow(merkez1y,2)));
                if(enbuyuk2<uzunluk2)
                {
                    enbuyuk2=uzunluk2;
                }
                if(yaricap1<enbuyuk2)
                {
                    yaricap1=yaricap1+0.1;
                    arttir=arttir+0.1;
                }
            }
            }

            printf("Son yari capi:%.2f\n",yaricap1);
            printf("Son merkez noktasi:{%.2f,%.2f}",merkez1x+arttir,merkez1y+arttir);
            int uzunluk3;
            int enkucuk3=0;
            for(i=0;i<sayac-1;i++)
            {
                  line((getmaxx()/2)+(noktalar[i].x*20),((getmaxy()/2)+(noktalar[i].y*20)),((getmaxx()/2)+(noktalar[i+1].x*20)),((getmaxy()/2)+(noktalar[i+1].y*20)));
            }
            circle((getmaxx()/2)+((merkez1x+arttir)*20),(getmaxy()/2)-((merkez1y+arttir)*20),yaricap1*20);
            getch();
            closegraph();
        }

    }
}
//-------------------------------------------------------------------//

Minimum enclosing circle can be drawn with this but it doesn't show a curve. I need to show curves without libraries.

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    C and C++ are different language. Would you mind choosing one? Also, your usage of `while(!feof(noktaal))` looks [wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) and you should check if readings are successful before using what is "read". – MikeCAT Oct 29 '20 at 12:01
  • I am using c language and I take the points from txt file. That's why I used while(!feof(noktaal)). I just need draw a curves. Code is correct only missing. – Cihan Icelliler Oct 29 '20 at 12:04
  • 1
    why [while(!feof(,,)](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) is always wrong – user3629249 Oct 29 '20 at 12:39
  • since *I am using c language*, please correct the *tags* to the question – user3629249 Oct 29 '20 at 12:41
  • OT: for ease of readability and understanding: (the compiler does not care) please consistently indent the code. Indent after every opening brace `{`. Unindent before every closing brace `}`. Suggest each indent level be 4 spaces. – user3629249 Oct 29 '20 at 12:44
  • the header file: `graphics.h` is a DOS header file. DO NOT use it in a windows nor linux environment. – user3629249 Oct 29 '20 at 12:48
  • I am sory for confusions. I'll fix the confusions soon as soon. – Cihan Icelliler Oct 29 '20 at 12:50
  • OT: regarding: `#include ` Nothing from that header file is being used in the posted code. It is a very poor programming practice to include header files those contents are not being used. – user3629249 Oct 29 '20 at 12:52
  • regarding: `#include ` This is a C++ header file, but your programming in C. Suggest removing that statement – user3629249 Oct 29 '20 at 12:54
  • I use graphics.h because the profesor wants me to use it – Cihan Icelliler Oct 29 '20 at 12:55
  • I am gonna remove vector and assert .Thanks for your comment. – Cihan Icelliler Oct 29 '20 at 12:56
  • regarding: `uzunluk2=sqrt((pow(noktalar[j].x,2)+pow(noktalar[j].y,2))-(pow(merkez1x,2)+pow(merkez1y,2)));` The syntax for `sqrt()` is: `double sqrt(double x);` but `uzunluk2` is a `float`. Suggest: `float sqrtf(float x);` regarding: `pow()` the field: `noktalar[j].y,` is an integer. for simplicity suggest using: `float powf(float x, float y);` – user3629249 Oct 29 '20 at 13:01
  • regarding: `pow(noktalar[j].y,2)` this kind of statement means: `noktalar[j].y * noktalar[j].y` which does not require any implicit conversions – user3629249 Oct 29 '20 at 13:05
  • X and y should be integer for my project. I also want to use float but the profesor wants me to use integer. I just want to draw a curves for dots now. Finally I am gonna check and fixs the wrong sentences. – Cihan Icelliler Oct 29 '20 at 13:06
  • I use pow because it is more understandable for me. But you are right I could write noktalar[j].y * noktalar[j].y like this – Cihan Icelliler Oct 29 '20 at 13:08
  • OT: when compiling, always enable the warnings, then fix those warnings. ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Note: other compilers use different options to produce the same results. – user3629249 Oct 29 '20 at 13:11
  • suggest to use `powf()` because `sqrt()` is expecting `float` not `double` – user3629249 Oct 29 '20 at 13:14
  • P:lease do not modify the initial posted code when comments are given. Rather, add a 'EDIT' section with the new code. Otherwise the original comments lend to confusion and become meaningless. – user3629249 Oct 29 '20 at 13:19

0 Answers0