File:Dyadic trans.gif

From testwiki
Jump to navigation Jump to search
    Original file (300 × 300 pixels, file size: 114 KB, MIME type: image/gif)

    This file is from Wikimedia Commons and may be used by other projects. The description on its file description page there is shown below.

    Summary

    Description
    English: Dyadic transformation xy plot
    Date
    Source Own work
    Author Claudio Rocchini
    Permission
    (Reusing this file)
    own work, attribution required

    Source Code

    /*****************************
     * Dyadic transformation Plot
     * (C)2010 Claudio Rocchini
     * claudio@rockini.name
     *****************************/
    
    #include <stdio.h>
    #include <assert.h>
    #include <vector>
    #include <set>
    
    typedef unsigned char byte;
    
    class image {			// image definition
    public:
    	byte * mbuf;		// Image memory
    	int    dimx,dimy;	// Image size
    	 image( int ndimx, int ndimy ) : dimx(ndimx),dimy(ndimy) {
    		 mbuf = new byte[dimx*dimy*3];
    		 memset(mbuf,255,dimx*dimy*3);
    	 }
    	~image() { delete[] mbuf; }
    	bool save_ppm( const char * filename ) const {
    		FILE * f = fopen(filename,"wb"); if(f==0) return false;
    		fprintf(f,"P6\n%d %d\n255\n",dimx,dimy);
    		fwrite(mbuf,3,dimx*dimy,f);
    		fclose(f);
    		return true;
    	}
    	void set_black_pixel( int x, int y, double trasp )
    	{
    		if(x<0 || x>=dimx) return; if(y<0 || y>=dimy) return;
    		byte * p = mbuf+3*(x+y*dimx);
    		p[0] = byte(p[0]*(1-trasp) /*+ (trasp)*0*/);
    		p[1] = byte(p[1]*(1-trasp) /*+ (trasp)*0*/);
    		p[2] = byte(p[2]*(1-trasp) /*+ (trasp)*0*/);
    	}
    	void dot( double x, double y, double trasp )	// float precision put pixel
    	{
    		int ix = int(x); double rx = x-ix;
    		int iy = int(y); double ry = y-iy;
    		set_black_pixel(ix+0,iy+0,trasp*(1-rx)*(1-ry));
    		set_black_pixel(ix+1,iy+0,trasp*(  rx)*(1-ry));
    		set_black_pixel(ix+0,iy+1,trasp*(1-rx)*(  ry));
    		set_black_pixel(ix+1,iy+1,trasp*(  rx)*(  ry));
    	}
    };
    
    	// rational definition
    typedef __int64 bigint;
    
    bigint gcd(bigint m, bigint n) {
    	while (n != 0){
    		bigint t = m % n;
    		m = n; n = t;
    	}
    	return m;
    }
    
    class rational {
    public:
    	bigint p,q;
    
    	rational(){}
    	rational( int np, int nq ) { p=np; q=nq; normalize(); }
    	bool operator< ( const rational & x ) const { return p*x.q < q*x.p; }
    	void normalize()  { bigint g = gcd(p,q); p /= g; q /= g; }
    	void doubled()    {
    		if( q%2==0 ) { assert(q!=1);        q /= 2; }		// assert for overflow checking
    		else         { assert(p< (1<<30) ); p *= 2; }
    	}
    	void modulo()     { while( p >= q ) p -= q; normalize(); }
    	operator double() { return double(p)/q; }
    };
    
    int main() {
    	const int SX = 600;
    	const int SY = 600;
    	image im(SX,SY);
    	std::set< rational > ss;					// fraction done
    	for(int i=2;i<150;++i) {					// enumerate the fractions
    		for(int j=1;j<i;++j) {
    			rational w(j,i);				// current start fractions
    			if( ss.find(w)!=ss.end() ) continue;		// just do it!
    			ss.insert(w);
    			double xx = SX*w;
    			std::set< rational > tt;			// row set
    			for(;;){
    				if( tt.find(w)!=tt.end() ) break;	// just do it: is a loop
    				tt.insert(w);
    				double yy = SX*w;
    				im.dot(xx,yy,0.5);			// dot this value
    				w.doubled();				// ww = (w*2) mod 1
    				w.modulo();
    			}
    		}
    	}
    	im.save_ppm("c:\\temp\\dyadic.ppm");
    	return 0;
    }
    

    Licensing

    I, the copyright holder of this work, hereby publish it under the following licenses:
    GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License.
    w:en:Creative Commons
    attribution share alike
    This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license.
    You are free:
    • to share – to copy, distribute and transmit the work
    • to remix – to adapt the work
    Under the following conditions:
    • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
    • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
    This licensing tag was added to this file as part of the GFDL licensing update.
    w:en:Creative Commons
    attribution
    This file is licensed under the Creative Commons Attribution 2.5 Generic license.
    You are free:
    • to share – to copy, distribute and transmit the work
    • to remix – to adapt the work
    Under the following conditions:
    • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
    You may select the license of your choice.

    Captions

    Add a one-line explanation of what this file represents

    Items portrayed in this file

    depicts

    12 December 2006

    116,703 byte

    300 pixel

    300 pixel

    image/gif

    f4a9c6f997d98565ecbbb2bbe6b30d8262cb23b0

    File history

    Click on a date/time to view the file as it appeared at that time.

    Date/TimeThumbnailDimensionsUserComment
    current14:02, 12 December 2006Thumbnail for version as of 14:02, 12 December 2006300 × 300 (114 KB)wikimediacommons>Rocchini{{Information| |Description=Dyadic transformation xy plot |Source=Own work |Date=2006-12-12 |Author=Claudio Rocchini |Permission=own work, attribution required }}

    There are no pages that use this file.