ITeye博客

android绘图坐标

yinter · 2009-11-23 18:01 · android · Markdown

在android中,手机的屏幕大少为320*480,原点坐标在左上角(0,0),在绘制图形时一定要非常清楚要绘制的图形对坐标转换的关系。

简单的图形如,直线,画点,画圆大家可能不会有什么问题,但如果遇到复杂图形或图形旋转等情况时,比较游戏俄罗斯方块中,对不同组合的方块进行变换时,涉及到很多坐标变换情况,如果不是完全清楚的话,很难编码了。

如下:

  public int[][] rotateStone(int[][] currentPosition) {
  	int[][] newPosition = new int[4][2];
  	int x, y;
  	switch (blockID) {
  	case 2:
  		if (rotatePosition == 0) {//一横变一竖
  			x = currentPosition[3][0];
  			y = currentPosition[3][1];
  			newPosition[0][0] = x - 3;
  			newPosition[0][1] = y;
  			newPosition[1][0] = x - 2;
  			newPosition[1][1] = y;
  			newPosition[2][0] = x - 1;
  			newPosition[2][1] = y;
  			newPosition[3][0] = x;
  			newPosition[3][1] = y;
  			rotatePosition = 1;
  		} else {//反之
  			x = currentPosition[3][0];
  			y = currentPosition[3][1];
  			newPosition[0][0] = x;
  			newPosition[0][1] = y - 3;
  			newPosition[1][0] = x;
  			newPosition[1][1] = y - 2;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y - 1;
  			newPosition[3][0] = x;
  			newPosition[3][1] = y;
  			rotatePosition = 0;
  		}
  		return newPosition;
  	case 3:
  		if (rotatePosition == 0) {//左T
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];
  			newPosition[0][0] = x;
  			newPosition[0][1] = y - 1;
  			newPosition[1][0] = x;
  			newPosition[1][1] = y + 1;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y;
  			newPosition[3][0] = x + 1;
  			newPosition[3][1] = y;
  			rotatePosition = 1;
  		} else if (rotatePosition == 1) {//上T
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];
  			newPosition[0][0] = x;
  			newPosition[0][1] = y + 1;
  			newPosition[1][0] = x - 1;
  			newPosition[1][1] = y;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y;
  			newPosition[3][0] = x + 1;
  			newPosition[3][1] = y;
  			rotatePosition = 2;
  		} else if (rotatePosition == 2) {//右T
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];
  			newPosition[0][0] = x;
  			newPosition[0][1] = y - 1;
  			newPosition[1][0] = x - 1;
  			newPosition[1][1] = y;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y;
  			newPosition[3][0] = x;
  			newPosition[3][1] = y + 1;
  			rotatePosition = 3;
  		} else if (rotatePosition == 3) {//下T
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];
  			newPosition[0][0] = x - 1;
  			newPosition[0][1] = y;
  			newPosition[1][0] = x + 1;
  			newPosition[1][1] = y;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y;
  			newPosition[3][0] = x;
  			newPosition[3][1] = y - 1;
  			rotatePosition = 0;
  		}
  		return newPosition;
  	case 4:
  		if (rotatePosition == 0) {//上右7
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];
  			newPosition[0][0] = x;
  			newPosition[0][1] = y;
  			newPosition[1][0] = x + 1;
  			newPosition[1][1] = y;
  			newPosition[2][0] = x + 2;
  			newPosition[2][1] = y;
  			newPosition[3][0] = x + 2;
  			newPosition[3][1] = y - 1;
  			rotatePosition = 1;
  		} else if (rotatePosition == 1) {//左上T
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];
  			newPosition[0][0] = x - 1;
  			newPosition[0][1] = y - 2;
  			newPosition[1][0] = x;
  			newPosition[1][1] = y - 2;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y - 1;
  			newPosition[3][0] = x;
  			newPosition[3][1] = y;
  			rotatePosition = 2;
  		} else if (rotatePosition == 2) {//下左7
  			x = currentPosition[1][0];
  			y = currentPosition[1][1];
  			newPosition[0][0] = x - 2;
  			newPosition[0][1] = y + 1;
  			newPosition[1][0] = x - 2;
  			newPosition[1][1] = y;
  			newPosition[2][0] = x - 1;
  			newPosition[2][1] = y;
  			newPosition[3][0] = x;
  			newPosition[3][1] = y;
  			rotatePosition = 3;
  		} else if (rotatePosition == 3) {//左上
  			x = currentPosition[1][0];
  			y = currentPosition[1][1];
  			newPosition[0][0] = x;
  			newPosition[0][1] = y;
  			newPosition[1][0] = x;
  			newPosition[1][1] = y + 1;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y + 2;
  			newPosition[3][0] = x + 1;
  			newPosition[3][1] = y + 2;
  			rotatePosition = 0;
  		}
  		return newPosition;
  	case 5:
  		if (rotatePosition == 0) {
  			x = currentPosition[1][0];
  			y = currentPosition[1][1];
  			newPosition[0][0] = x - 2;
  			newPosition[0][1] = y - 1;
  			newPosition[1][0] = x - 1;
  			newPosition[1][1] = y - 1;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y - 1;
  			newPosition[3][0] = x;
  			newPosition[3][1] = y;
  			rotatePosition = 1;
  		} else if (rotatePosition == 1) {
  			x = currentPosition[0][0];
  			y = currentPosition[0][1];
  			newPosition[0][0] = x;
  			newPosition[0][1] = y;
  			newPosition[1][0] = x + 1;
  			newPosition[1][1] = y;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y + 1;
  			newPosition[3][0] = x;
  			newPosition[3][1] = y + 2;
  			rotatePosition = 2;
  		} else if (rotatePosition == 2) {
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];
  			newPosition[0][0] = x;
  			newPosition[0][1] = y;
  			newPosition[1][0] = x;
  			newPosition[1][1] = y + 1;
  			newPosition[2][0] = x + 1;
  			newPosition[2][1] = y + 1;
  			newPosition[3][0] = x + 2;
  			newPosition[3][1] = y + 1;
  			rotatePosition = 3;
  		} else if (rotatePosition == 3) {
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];
  			newPosition[0][0] = x;
  			newPosition[0][1] = y;
  			newPosition[1][0] = x + 1;
  			newPosition[1][1] = y;
  			newPosition[2][0] = x + 1;
  			newPosition[2][1] = y - 1;
  			newPosition[3][0] = x + 1;
  			newPosition[3][1] = y - 2;
  			rotatePosition = 0;
  		}
  		return newPosition;
  	case 6:
  		if (rotatePosition == 0) {
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];
  			newPosition[0][0] = x - 1;
  			newPosition[0][1] = y;
  			newPosition[1][0] = x;
  			newPosition[1][1] = y + 1;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y;
  			newPosition[3][0] = x + 1;
  			newPosition[3][1] = y + 1;
  			rotatePosition = 1;
  		} else {
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];

  			newPosition[0][0] = x;
  			newPosition[0][1] = y - 1;
  			newPosition[1][0] = x - 1;
  			newPosition[1][1] = y;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y;
  			newPosition[3][0] = x - 1;
  			newPosition[3][1] = y + 1;
  			rotatePosition = 0;
  		}
  		return newPosition;
  	case 7:
  		if (rotatePosition == 0) {
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];

  			newPosition[0][0] = x - 1;
  			newPosition[0][1] = y - 1;
  			newPosition[1][0] = x - 1;
  			newPosition[1][1] = y;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y;
  			newPosition[3][0] = x;
  			newPosition[3][1] = y + 1;
  			rotatePosition = 1;
  		} else {
  			x = currentPosition[2][0];
  			y = currentPosition[2][1];

  			newPosition[0][0] = x + 1;
  			newPosition[0][1] = y;
  			newPosition[1][0] = x - 1;
  			newPosition[1][1] = y + 1;
  			newPosition[2][0] = x;
  			newPosition[2][1] = y;
  			newPosition[3][0] = x;
  			newPosition[3][1] = y + 1;
  			rotatePosition = 0;
  		}
  		return newPosition;
  	default:
  		return currentPosition;

  	}

  }

以上代码就是俄罗斯方块中的图形变换。

这是其一,另外还要找准其旋转坐标点,方程变换。

(x,y), (x+1,y),(x+2, y)即为向屏幕x轴移动,(x,y),(x,y+1),(x,y+2)即为向屏幕y轴移动,其它一此类推。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。