esercizio 1

« Older   Newer »
 
  Share  
.
  1. lumo
     
    .

    User deleted


    Inauguro la sezione !!
    Propongo un semplice esercizio che avevo fatto un pò di tempo fa...
    Scrivere un programma che generi in output un file ppm con un'immagine di dimensione arbitraria contenente una croce nel mezzo.

    Il PPM è un formato di immagine molto semplice. Ne esistono di vari tipi, ne descrivo solo uno:

    è formato da un'intestazione in ascii

    CODICE
    P3
    # commento opzionale
    larghezza altezza
    255

    per esempio un'immagine 20x30 pixel sarà

    CODICE
    P3
    # commento opzionale
    20 30
    255


    Dopo l'intestazione va un whitespace (di solito si va a capo) e poi seguono i dati sottoforma di terne RGB.
     
    Top
    .
  2. lumo
     
    .

    User deleted


    scusate mi sono dimenticato di mettere un esempio di output:
    image
    I colori non devono essere per forza quelli :P
     
    Top
    .
  3. lumo
     
    .

    User deleted


    lol scusate dell'immagine di prima (tarocca XD) ecco:
    image
     
    Top
    .
  4. lumo
     
    .

    User deleted


    suuusususu siete degli scansafatiche XD
    forse siete troppo n00b >_>
     
    Top
    .
  5. fabi3194
     
    .

    User deleted


    CITAZIONE (lumo @ 19/11/2008, 21:49)
    suuusususu siete degli scansafatiche XD
    forse siete troppo n00b >_>

    :rotfl: :rotfl: :rotfl: :rotfl:
     
    Top
    .
  6. lumo
     
    .

    User deleted


    CITAZIONE (fabi3194 @ 19/11/2008, 22:10)
    CITAZIONE (lumo @ 19/11/2008, 21:49)
    suuusususu siete degli scansafatiche XD
    forse siete troppo n00b >_>

    :rotfl: :rotfl: :rotfl: :rotfl:

    cosa c'è da ridere se sei n00b ? XD
    comunque l'ho fatto appena adessso in C++....su posta il tuo in Java ^_^
    CODICE
    #include <iostream>
    #include <fstream>
    #include <cstring>

    using namespace std;

    class PPMImage {
       private:
           int h,w,s;
           fstream img;
       public:
           PPMImage(int ,int ,int );
           ~PPMImage(void);
           void writeCross(void);
    };

    PPMImage::PPMImage(int h,int w,int s)
    {
       this->h=h;
       this->w=w;
       this->s=s;
       img.open("croce.ppm",ios::binary | ios::out);
       img << "P3\n#croce\n" << h << " " << w << "\n255" << endl;
    }

    PPMImage::~PPMImage(void)
    {
       img.close();
    }

    void PPMImage::writeCross(void)
    {
       for (int i=0;i<h;i++) {
           for (int j=0;j<w;j++) {
               img << ((((i<(h+s)/2) && (i>(h-s)/2)) || ((j<(w+s)/2) && (j>(w-s)/2))) ? "255\n255\n255\n" : "000\n000\n000\n");
           }
       }
    }

    int main(void)
    {
       int hw,s;
       cout << "larghezza-altezza => "; cin >> hw;
       cout << "spessore=> "; cin >> s;
       PPMImage img(hw,hw,s);
       img.writeCross();
       return 0;
    }
     
    Top
    .
  7. ^Enrix^
     
    .

    User deleted


    Oddio mi sono dimenticato di postare la soluzione! :rotfl:
    eccola (in python ovviamente):

    CODICE
    from time import *
    tmp=time()
    altz,largh,cm,nm=input("Altezza ->"),input("Larghezza ->"),raw_input("Commento ->"),raw_input("Nome file ->")
    file=open(nm+".ppm","w")
    if cm[:1] != "#": cm="#"+cm
    hd=("P3",cm,str(largh)+chr(32)+str(altz),"\n255\n")
    file.write("\n".join(hd))
    for height in range(0,altz):
      for width in range(0,largh):
         if height==altz/2 or width==largh/2:
            file.write("255\n255\n255\n")
         else:
            file.write("000\n000\n000\n")
    print nm,"generato in %­s secondi" %­str((time()-tmp))
    file.close()


    potrebbe esserci qualche bug :rotfl:
     
    Top
    .
  8. lumo
     
    .

    User deleted


    CITAZIONE (^Enrix^ @ 11/12/2008, 21:58)
    Oddio mi sono dimenticato di postare la soluzione! :rotfl:
    eccola (in python ovviamente):

    CODICE
    from time import *
    tmp=time()
    altz,largh,cm,nm=input("Altezza ->"),input("Larghezza ->"),raw_input("Commento ->"),raw_input("Nome file ->")
    file=open(nm+".ppm","w")
    if cm[:1] != "#": cm="#"+cm
    hd=("P3",cm,str(largh)+chr(32)+str(altz),"n255n")
    file.write("n".join(hd))
    for height in range(0,altz):
      for width in range(0,largh):
         if height==altz/2 or width==largh/2:
            file.write("255n255n255n")
         else:
            file.write("000n000n000n")
    print nm,"generato in %­s secondi" %­str((time()-tmp))
    file.close()


    potrebbe esserci qualche bug :rotfl:

    l'ho provato e va XD
    Comqunqe prova a mettere un opzione per la larghezza della croce XD
     
    Top
    .
  9. ^Enrix^
     
    .

    User deleted


    Ok, in questi giorni cercherò di trovare qualche ritaglio di tempo per modificare il codice ^_^
     
    Top
    .
  10. D!alga™
     
    .

    User deleted


    beata ignoranza, nn ho capito un caxo :D
     
    Top
    .
  11. fabi3194
     
    .

    User deleted


    CITAZIONE (D!alga™ @ 19/12/2008, 19:05)
    beata ignoranza, nn ho capito un caxo :D

    :rotfl: :rotfl: :rotfl: :rotfl:
     
    Top
    .
  12. lumo
     
    .

    User deleted


    :rotfl: non è cosi difficile
     
    Top
    .
  13. lumo
     
    .

    User deleted


    fatto un piccolo miglioramento
    CODICE
    #include <iostream>
    #include <fstream>

    using namespace std;

    class PPMImage {
      private:
          int h,w,s;
          fstream img;
      public:
          PPMImage(int ,int ,int );
          ~PPMImage(void);
          void writeCross(void);
    };

    PPMImage::PPMImage(int h,int w,int s)
    {
      this->h=h;
      this->w=w;
      this->s=s;
      img.open("croce.ppm",ios::binary | ios::out);
      img << "P3\n#croce\n" << h << " " << w << "\n255" << endl;
    }

    PPMImage::~PPMImage(void)
    {
      img.close();
    }

    void PPMImage::writeCross(void)
    {
      int i,j;
      bool cond;
      for (i=0;i<h;i++) {
          cond=((i<(h+s)/2) & (i>(h-s)/2));
          for (j=0;j<w;j++) {
              img << (( cond | ((j<(w+s)/2) & (j>(w-s)/2))) ? "255\n255\n255\n" : "000\n000\n000\n");
          }
      }
    }

    int main(void)
    {
      int hw,s;
      cout << "larghezza-altezza => "; cin >> hw;
      cout << "spessore=> "; cin >> s;
      PPMImage img(hw,hw,s);
      img.writeCross();
      return 0;
    }
     
    Top
    .
  14. ^Enrix^
     
    .

    User deleted


    Update :
    CODICE
    from time import *
    tmp=time()
    altz,largh,sps,cm,nm=input("Altezza ->"),input("Larghezza ->"),input("Spessore ->"),raw_input("Commento ->"),raw_input("Nome file ->")
    file=open(nm+".ppm","w")
    if cm[:1] != "#": cm="#"+cm
    hd=("P3",cm,str(largh)+chr(32)+str(altz),"\n255\n")
    file.write("\n".join(hd))
    for height in range(0,altz):
     for width in range(0,largh):
        if (altz-(sps/2))/2 <= height <= (altz+(sps/2))/2 or (largh-(sps/2))/2<= width <= (largh+(sps/2))/2: file.write("255\n255\n255\n")
        else: file.write("000\n000\n000\n")
    print nm,"generato in %­s secondi" %­str(time()-tmp)
    file.close()
     
    Top
    .
  15. fabi3194
     
    .

    User deleted


    Ma è python o ASM? :rotfl: :rotfl: :rotfl:
     
    Top
    .
25 replies since 16/11/2008, 20:14   578 views
  Share  
.