写了一个if...else的方法,功能是实现了,但是感觉不太好看...谁能帮我转换一个成算法,万分感谢

lz1201048 2009-04-07 01:01:19
        /// <summary>
/// 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);
}
...全文
320 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaxiazhu119 2009-04-08
  • 打赏
  • 举报
回复
感觉还是别看代码了 回到原点
LZ先说要实现啥吧 然后再看怎么写代码
llsen 2009-04-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 justindreams 的回复:]
C# code
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

[/Quote]
这样的用switch好些
阿非 2009-04-08
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 Sandy945 的回复:]
C# code
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++…
[/Quote]


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());

测试通过
阿非 2009-04-08
  • 打赏
  • 举报
回复

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 );
}

你试试吧
meheartfly 2009-04-08
  • 打赏
  • 举报
回复
鄙视只会敲键盘,不会动脑子的懒人!
meheartfly 2009-04-08
  • 打赏
  • 举报
回复
真正的程序员是脑力劳动,伪程序员是体力劳动者。从这个例子就可以看出很多人是否是一个合格的程序员!
南哥1207 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 yuehuolong 的回复:]
找到intDirection 和new Vector2(a,b)中参数的关系,就可以一句实现
[/Quote]
假设:
假设:
找到了关系 intDirection=a*x=b*y
那就可以用一句话new Vector2(intDirection/x,intDirection/y)实现了
不知道可不可以实现
南哥1207 2009-04-07
  • 打赏
  • 举报
回复
找到intDirection 和new Vector2(a,b)中参数的关系,就可以一句实现
liuyang052 2009-04-07
  • 打赏
  • 举报
回复
switch
LemIST 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 lz1201048 的回复:]
16,17楼非常接近了,关注
PS:就算以后写可以返回1000个方向的方法,也可以分解4个部分:绕坐标轴右上,右下,左下,左上4种
绝对能写出比较简单的算法,我自己也再看看
[/Quote]
明显是个圆,可以从这方面入手。具体的题目楼主能够给出么?我们要算什么?方向代表什么意思?
freewind0521 2009-04-07
  • 打赏
  • 举报
回复
如果没有规律的话,只能用switch一个个列举了
lz1201048 2009-04-07
  • 打赏
  • 举报
回复
16,17楼非常接近了,关注
PS:就算以后写可以返回1000个方向的方法,也可以分解4个部分:绕坐标轴右上,右下,左下,左上4种
绝对能写出比较简单的算法,我自己也再看看
meheartfly 2009-04-07
  • 打赏
  • 举报
回复
 VS2008破解方法非常简单,在开始>设置>控制面版>添加或删除程序>卸载vs.net2008>出现卸载界面>点击Next>输入上面CD-key ->出现成功画面即可完美将试用版升级成为正式版。

VS2008正式版序列号CDKEY:PYHYP-WXB3B-B2CCM-V9DX9-VDY8T
meheartfly 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wartim 的回复:]
你这个就是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 P…
[/Quote]

有规律的,很明显要写循环的
LemIST 2009-04-07
  • 打赏
  • 举报
回复
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))
);
wartim 2009-04-07
  • 打赏
  • 举报
回复
你这个就是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 同理自己算吧,都是由规律的
LemIST 2009-04-07
  • 打赏
  • 举报
回复
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)?
ztenv 2009-04-07
  • 打赏
  • 举报
回复
可以看看《代码大全》的“表驱动法”
ztenv 2009-04-07
  • 打赏
  • 举报
回复
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的实例并返回;
}
}
lz1201048 2009-04-07
  • 打赏
  • 举报
回复
6楼的能理解我的意思...要是1000个方向呢,就写1000个case吗...
加载更多回复(11)

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧