写了一个if...else的方法,功能是实现了,但是感觉不太好看...谁能帮我转换一个成算法,万分感谢
/// <summary>--------------------编程问答-------------------- 用switch不行吗?没大看懂。 --------------------编程问答--------------------
/// 16个方向
/// 0.5,1,1.5
/// </summary>
/// <param name="intDirection"></param>
/// <returns></returns>
private Vector2 Vector216DirectionSpeed2_3(int intDirection)
{
float floatPiDivide360 = MathHelper.Pi / 360;
float float25 = floatPiDivide360 * 22.5f;
float float45 = floatPiDivide360 * 45.0f;
if (intDirection == 0)
return new Vector2(0.0f, -2.0f);//0°
else if (intDirection == 1)
return new Vector2(0.5f + float25 + float25 / 2,//22.5°
-1.5f - float25 - float25 / 2);
else if (intDirection == 2)
return new Vector2(1.0f + float45,//45°
-1.0f - float45);
else if (intDirection == 3)
return new Vector2(1.5f + float25 + float25 / 2, //67.5°
-0.5f - float25 - float25 / 2);
else if (intDirection == 4)
return new Vector2(2.0f, 0.0f);//90°
else if (intDirection == 5)
return new Vector2(1.5f + float25 + float25 / 2,//112.5°
0.5f + float25 + float25 / 2);
else if (intDirection == 6)
return new Vector2(1.0f + float45,//135°
1.0f + float45);
else if (intDirection == 7)
return new Vector2(0.5f + float25 + float25 / 2,//157.5°
1.5f + float25 + float25 / 2);
else if (intDirection == 8)
return new Vector2(-0.0f, 2.0f);//180°
else if (intDirection == 9)
return new Vector2(-0.5f - float25 - float25 / 2,//202.5°
1.5f + float25 + float25 / 2);
else if (intDirection == 10)
return new Vector2(-1.0f - float45,//225°
1.0f + float45);
else if (intDirection == 11)
return new Vector2(-1.5f - float25 - float25 / 2,//247.5°
0.5f + float25 + float25 / 2);
else if (intDirection == 12)
return new Vector2(-2.0f, 0.0f);//270°
else if (intDirection == 13)
return new Vector2(-1.5f - float25 - float25 / 2,//292.5°
-0.5f - float25 - float25 / 2);
else if (intDirection == 14)
return new Vector2(-1.0f - float45,//315°
-1.0f - float45);
else if (intDirection == 15)
return new Vector2(-0.5f - float25 - float25 / 2,//337.5°
-1.5f - float25 - float25 / 2);
else
return new Vector2(0, 0);
}
--------------------编程问答-------------------- 用switch呗 --------------------编程问答-------------------- 我就想用一行代码代替上面的40多行代码 --------------------编程问答-------------------- 楼主,大家上班上学很累的,这种体力活的话,自己来吧~~ --------------------编程问答-------------------- 那1000个呢?也这样写? --------------------编程问答-------------------- 用switch case 语句代替。 --------------------编程问答-------------------- 这种语句一样用的是switch
private Vector2 Vector216DirectionSpeed2_3(int intDirection)
{
float floatPiDivide360 = MathHelper.Pi / 360;
float float25 = floatPiDivide360 * 22.5f;
float float45 = floatPiDivide360 * 45.0f;
float var1,var2;
switch(intDirection)
{
case 0:var1=....;var2=....;break;//这个你自己算吧
//一直case到15
default:var1=0f;var2=0f;break;
}
return new Vector2(var1,var2);
}
或者用数组搞掂
float[] x= new int {0.0f,0.5f + float25 + float25,1.0f + float45,...};
float[] y= new int {2.0f,-1.5f - float25 - float25 / 2,-1.0f - float45,...};
return new Vector2(x[intDirection],[intDirection]);
-------------------------
或者
数据看样子也是规律的,完全可以计算搞掂,不用这样if ... else
--------------------编程问答--------------------
现在是白天呢~~
除非你15个方向返回的数值能用一个公式和一个变量进行替换,否则只能用switch列举
--------------------编程问答-------------------- 楼主,你这返回的值也没什么规律,我看你用什么方法,代码也得多多的 --------------------编程问答-------------------- 6楼的能理解我的意思...要是1000个方向呢,就写1000个case吗... --------------------编程问答--------------------
你的那两个参数有什么规律?
如果没有规律,怎么能给你一行代码解决问题的方法?
--------------------编程问答-------------------- class DataPoint
{
public float X{get;set;}
public float Y{get;set;}
}
public class VectorFactory
{
private IDictionary<int,DataPoint> m_Vector=new Dictionary<int,DataPoint>(16);
private VectorFactor()
{
//初始化字典
m_Vector.add(0,new DataPoint(0.0f, -2.0f));
.....
// m_Vector.add(15,new DataPoint());//第16个转换的数据
}
public Vector2 GetVector(int Direction)
{
//根据Direction从字典m_Vector取得DataPoint的实例,
//然后创建Vector2的实例并返回;
}
} --------------------编程问答-------------------- 可以看看《代码大全》的“表驱动法” --------------------编程问答-------------------- else if (intDirection == 15)
return new Vector2(-0.5f - float25 - float25 / 2,//337.5°
-1.5f - float25 - float25 / 2);
else
return new Vector2(0, 0);
是否应该是return new Vector2(0, -2.0f)? --------------------编程问答-------------------- 你这个就是4部分组成,当然,P3,P4在4的倍数时就不填
return new Vector2(P1+P2, P3+P4)
intDirection P1 P2 , P3 P4
n=0 0*0.5f 0
1 1*0.5f 1.5*float25 (-3)*0.5f P2*(-1)
2 2*0.5f 2*float25 (-2)*0.5f P2*(-1)
3 3*0.5f 1.5*float25 (-1)*0.5f P2*(-1)
4 4*0.5f 0
5 3*0.5f 1.5*float25 1*0.5f P2*1
6 2*0.5f 2*float25 2*0.5f P2*1
7 1*0.5f 1.5*float25 3*0.5f P2*1
8 0*0.5f 0
9(1) (-1)*0.5f 1.5*float25 3*0.5f P2*(-1)
10(2) (-2)*0.5f 2*float25 2*0.5f P2*(-1)
11(4) (-3)*0.5f 1.5*float25 1*0.5f P2*(-1)
12(4) (-4)*0.5f 0
13(5) (-3)*0.5f 1.5*float25 (-1)*0.5f P2*1
14(6) (-2)*0.5f 2*float25 (-2)*0.5f P2*1
15(7) (-1)*0.5f 1.5*float25 (-3)*0.5f P2*1
P1=(n < 9 ? n % 5 * (1 - n / 5) + n / 5 * (8 - n) : -((n - 8) % 5 * (1 - (n-8) / 5) + (n - 8) / 5 * (16 - n)));
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
for (int n = 0; n < 16; n++)
{
int x = n < 9 ? n % 5 * (1 - n / 5) + n / 5 * (8 - n) : -((n - 8) % 5 * (1 - (n-8) / 5) + (n - 8) / 5 * (16 - n));
Console.WriteLine(n.ToString() + " " + x);
}
Console.Read();
}
}
}
0 0
1 1
2 2
3 3
4 4
5 3
6 2
7 1
8 0
9 -1
10 -2
11 -3
12 -4
13 -3
14 -2
15 -1
P2 P3 P4 同理自己算吧,都是由规律的 --------------------编程问答-------------------- return new Vector2((float)((intDircection > 8 ? -1f : 1f) * (2 - Math.Abs(intDircection % 8 - 4) * 0.5 + ((intDircection % 4 - 2) % 2 == 0 ? intDircection % 4 : 1.5) * (float)Math.PI / 16)),
(float)((Math.Abs(intDircection - 8) < 4 ? 1 : -1) * (Math.Abs(intDircection % 8 - 4) * 0.5 + ((intDircection % 4 - 2) % 2 == 0 ? intDircection % 4 : 1.5) * (float)Math.PI / 16))
); --------------------编程问答--------------------
有规律的,很明显要写循环的 --------------------编程问答-------------------- VS2008破解方法非常简单,在开始>设置>控制面版>添加或删除程序>卸载vs.net2008>出现卸载界面>点击Next>输入上面CD-key ->出现成功画面即可完美将试用版升级成为正式版。
VS2008正式版序列号CDKEY:PYHYP-WXB3B-B2CCM-V9DX9-VDY8T --------------------编程问答-------------------- 16,17楼非常接近了,关注
PS:就算以后写可以返回1000个方向的方法,也可以分解4个部分:绕坐标轴右上,右下,左下,左上4种
绝对能写出比较简单的算法,我自己也再看看 --------------------编程问答-------------------- 如果没有规律的话,只能用switch一个个列举了 --------------------编程问答--------------------
明显是个圆,可以从这方面入手。具体的题目楼主能够给出么?我们要算什么?方向代表什么意思? --------------------编程问答-------------------- switch --------------------编程问答-------------------- 找到intDirection 和new Vector2(a,b)中参数的关系,就可以一句实现
--------------------编程问答--------------------
假设:
假设:
找到了关系 intDirection=a*x=b*y
那就可以用一句话new Vector2(intDirection/x,intDirection/y)实现了
不知道可不可以实现 --------------------编程问答-------------------- 感觉还是别看代码了 回到原点
LZ先说要实现啥吧 然后再看怎么写代码 --------------------编程问答-------------------- 真正的程序员是脑力劳动,伪程序员是体力劳动者。从这个例子就可以看出很多人是否是一个合格的程序员! --------------------编程问答-------------------- 鄙视只会敲键盘,不会动脑子的懒人! --------------------编程问答--------------------
private Vector2 Vector216DirectionSpeed2_3(int intDirection)
{
float floatPiDivide360 = MathHelper.Pi / 360;
float float25 = floatPiDivide360 * 22.5f;
float float45 = floatPiDivide360 * 45.0f;
float flagOne = 0.0f;
float flagTwo = 0.0f;
float paOne = 0;
float paTwo = 0;
for (int i = 0; i < intDirection; i++)
{
if (i < 5)
{
flagOne = 0.5f * i + 0.0f;
flagTwo = -2.0f + 0.5f * i;
}
else
{
flagOne = flagOne - (i - 4) * 0.5f;
if (i < 9)
{
flagTwo = -2.0f + 0.5f * i;
}
else
{
flagTwo = flagTwo - (i - 8) * 0.5f;
}
}
}
if (-1 < intDirection < 16)
{
if (intDirection % 2 == 0 && intDirection % 4 != 0)
{
paOne = flagOne + float45;
paTwo = flagTwo - float45;
}
else if (intDirection % 4 == 0)
{
paOne = flagOne;
paTwo = flagTwo;
}
else if (intDirection < 9)
{
paOne = flagOne + float25 + float25 / 2;
if (intDirection > 4)
{
paTwo = flagTwo + float25 + float25 / 2;
}
else
{
paTwo = flagTwo - float25 - float25 / 2;
}
}
else
{
paOne = flagOne - float25 - float25 / 2;
if (intDirection > 12)
{
paTwo = flagTwo - float25 - float25 / 2;
}
else
{
paTwo = flagTwo + float25 + float25 / 2;
}
}
}
return new Vector2(paOne , paTwo );
}
你试试吧 --------------------编程问答--------------------
float float25 = 22.5f;
float float45 = 45.0f;
float flagOne = 0.0f;
float flagTwo = 0.0f;
float paOne = 0;
float paTwo = 0;
int intDirection = 15;
for (int i = 0; i <= intDirection; i++)
{
if (i < 5)
{
flagOne = 0.5f * i + 0.0f;
flagTwo = -2.0f + 0.5f * i;
}
else
{
if (i < 9)
{
flagOne -= 0.5f;
flagTwo = -2.0f + 0.5f * i;
}
else
{
flagTwo = flagTwo - 0.5f;
if (i > 12)
{
flagOne += 0.5f;
}
else
{
flagOne -= 0.5f;
}
}
}
}
if (-1 < intDirection && intDirection < 16)
{
if (intDirection % 2 == 0 && intDirection % 4 != 0)
{
paOne = flagOne + float45;
paTwo = flagTwo - float45;
}
else if (intDirection % 4 == 0)
{
paOne = flagOne;
paTwo = flagTwo;
}
else if (intDirection < 9)
{
paOne = flagOne + float25 + float25 / 2;
if (intDirection > 4)
{
paTwo = flagTwo + float25 + float25 / 2;
}
else
{
paTwo = flagTwo - float25 - float25 / 2;
}
}
else
{
paOne = flagOne - float25 - float25 / 2;
if (intDirection > 12)
{
paTwo = flagTwo - float25 - float25 / 2;
}
else
{
paTwo = flagTwo + float25 + float25 / 2;
}
}
}
Response.Write("paOne: " + paOne.ToString() + ",paTwo: " + paTwo.ToString());
测试通过 --------------------编程问答--------------------
这样的用switch好些
补充:.NET技术 , C#