Outward Spiral Algorithm

Discussion in 'Programming General' started by Govind, Oct 30, 2010.

Outward Spiral Algorithm
  1. Unread #1 - Oct 30, 2010 at 3:39 PM
  2. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Outward Spiral Algorithm

    This is very old code of mine that I've recently found after backing up stuff off my old computer. Added some comments to it; thought I'd post it here.

    Code:
    #include <windows.h>
    #define D_RIGHT 0
    #define D_UP 1
    #define D_LEFT 2
    #define D_DOWN 3
    typedef int DIRECTION;
    
    void WindowSpiral(HWND hWnd, int jumpdiv)
    {
    	/* Declarations */
    	RECT dimensions;  
    	int centerX, centerY, jumps = 1, i, curX, curY, xjumpamt, yjumpamt;
    	DIRECTION direction = D_RIGHT; 
    	POINT p;
    
    	/* Get the rect for dimension and jump configuration */
    	GetWindowRect(hWnd,&dimensions);
    	/* Integer division but at most we'd be off by one pixel; gets the center of the window */
    	centerX = dimensions.left+((dimensions.right-dimensions.left)/2); 
    	centerY = dimensions.top+((dimensions.bottom-dimensions.top)/2);
    	/* These will be the amount jumped each iteration; integer division but good enough */
    	xjumpamt = (dimensions.right-dimensions.left)/jumpdiv;
    	yjumpamt = (dimensions.bottom-dimensions.top)/jumpdiv;
    
    	/* Get into the middle of the window before we start */
    	curX = centerX; curY = centerY;
    	SetCursorPos(curX,curY);
    	while(1)
    	{
    		//if(direction < 3) direction = D_RIGHT;
    		for(i = 0; i < jumps; i++) 
    		{
    			switch(direction%4)
    			{
    				case D_RIGHT:
    					curX+=xjumpamt;
    					break;
    				case D_UP:
    					curY-=yjumpamt;
    					break;
    				case D_LEFT:
    					curX-=xjumpamt;
    					break;
    				case D_DOWN:
    					curY+=yjumpamt;
    					break;
    			}
    			SetCursorPos(curX,curY);
    			Sleep(50);
    			// The above two lines are NOT necessary; they are only so you can see the current location of the spiral loop.
    		}
    		jumps++; direction++; // increment jumps and direction each time around so that the spiral will grow outward
    
    		/* Check if we're out of the window rect; if so, no more*/
    		GetCursorPos(&p);
    		if(p.x >= dimensions.right) break;
    		if(p.y >= dimensions.bottom) break;
    		if(p.x <= dimensions.left) break;
    		if(p.y <= dimensions.top) break;
    
    	}	
    }
    From here, you can add GetPixel and change the return type to make a color clicker.

    The SetCursorPos/GetCursorPos and Sleep lines shouldn't be in a finished application; those are there so the cursor can be used to indicate the path of the spiral. To check if you're finished in an application without those lines, compare curX/curY with the dimension.top/bottom/left/right values.
     
  3. Unread #2 - Nov 4, 2010 at 6:47 AM
  4. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    Outward Spiral Algorithm

    Thanks for sharing, SMR!
     
  5. Unread #3 - Nov 14, 2010 at 7:57 PM
  6. Aeropsia
    Joined:
    May 11, 2010
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0

    Aeropsia Member

    Outward Spiral Algorithm

    I <3 you SMR!
     
  7. Unread #4 - Nov 14, 2010 at 8:39 PM
  8. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Outward Spiral Algorithm

    Haha, thanks :)
     
< Vector vs. Dynamic Memory | Svchost.exe has encountered a problem >

Users viewing this thread
1 guest


 
 
Adblock breaks this site