中点画圆算法

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class test : MonoBehaviour {



 Texture2D alphaTest;


    void Start () {
    
            alphaTest = (Texture2D)this.GetComponent<GUITexture>().texture;
            Debug.Log(Screen.width+"  "+Screen.height);
    }
    
    // Update is called once per frame
    void Update () {
    
        if(Input.GetMouseButton(0))
        {
            Debug.Log(Input.mousePosition);
            
            int x = (int)Input.mousePosition.x;
            int y = (int)Input.mousePosition.y;
            
            x=(x*1024)/Screen.width;
            y=(y*768)/Screen.height;
        
            Color cc = alphaTest.GetPixel(x,y);    
            Debug.Log(cc);
            
            alphaTest.SetPixel(x,y,new Color(1,0,0,1));
            
//            for(int i=0 ; i<30 ; ++i)
//            {
//                circleMidpoint(x,y,i,new Color(1,0,0,0));
//            }
            
            roundMidpoint(x,y,30,new Color(0,1,0,1));
            
            alphaTest.Apply();
            
            
            
        }    
    
    }
    
    
    ///把屏幕座标转换成NGUI的座标,此方法只适用于基于屏幕宽度缩放。参数:screenPosition - 鼠标或手指在屏幕上的座标。widthBased - 基于缩放的宽度是多少。
    public Vector2 convertScreenToNguiPositionBasedOnWidth(Vector3 screenPosition,float widthBased)
    {
    return new Vector2((screenPosition.x - Screen.width / 2f) * widthBased / Screen.width,(screenPosition.y - Screen.height / 2f) * widthBased / Screen.width);
    }
    
    ///把屏幕座标转换成左 → 右 , 上 ↓ 下的座标,此方法只适用于基于屏幕宽度缩放。参数:screenPosition - 鼠标或手指在屏幕上的座标。widthBased - 基于缩放的宽度是多少。
    public Vector2 convertScreenToTexture(Vector3 screenPosition,float widthBased)
    {
    return new Vector2(( screenPosition.x ) * widthBased / Screen.width,( Screen.height -screenPosition.y ) * widthBased / Screen.width);
    }
    
    
    
    
    private void circlePoints(int cx, int cy, int x, int y, Color pix)
    {
        
        if (x == 0) {
            alphaTest.SetPixel(cx, cy + y,pix );  
            alphaTest.SetPixel(cx, cy - y,pix );  
            alphaTest.SetPixel(cx + y, cy,pix );  
            alphaTest.SetPixel(cx - y, cy,pix );                 
        } else 
        if (x == y) {
            alphaTest.SetPixel(cx + x, cy + y,pix );
            alphaTest.SetPixel(cx - x, cy + y,pix );
            alphaTest.SetPixel(cx + x, cy - y,pix );
            alphaTest.SetPixel(cx - x, cy - y,pix );
        } else                            
        if (x < y) {                      
            alphaTest.SetPixel(cx + x, cy + y,pix );
            alphaTest.SetPixel(cx - x, cy + y,pix );
            alphaTest.SetPixel(cx + x, cy - y,pix );
            alphaTest.SetPixel(cx - x, cy - y,pix );
            alphaTest.SetPixel(cx + y, cy + x,pix );
            alphaTest.SetPixel(cx - y, cy + x,pix );
            alphaTest.SetPixel(cx + y, cy - x,pix );
            alphaTest.SetPixel(cx - y, cy - x,pix );
        }
    }

    public void circleMidpoint(int xCenter, int yCenter, int radius, Color c)
    {
        int x = 0;
        int y = radius;
        int p = (5 - radius*4)/4;

        circlePoints(xCenter, yCenter, x, y, c);
        while (x < y) {
            x++;
            if (p < 0) {
                p += 2*x+1;
            } else {
                y--;
                p += 2*(x-y)+1;
            }
            circlePoints(xCenter, yCenter, x, y, c);
        }
    }
    
    public void roundMidpoint(int xCenter, int yCenter, int radius, Color c)
    {
        for(int i=-radius ; i<=+radius ; ++i)
            {
                for(int j =-radius ; j<=+radius ; ++j)
                {
                    if((i*i+j*j)<=(radius*radius))
                            alphaTest.SetPixel(i+xCenter, j+yCenter,c);  
                }
            } 
    }

}

posted @ 2014-02-18 16:56  火之梦魇  阅读(1199)  评论(0编辑  收藏  举报