踢馆帝
踢馆帝原创小测试,第五题!

本帖最后由 踢馆帝 于 2012-6-23 16:12 编辑

由于搬砖很辛苦,很久没出题了,终于放假了,先出一个吧

三维空间中有两个球,球心坐标分别是L1,L2。 ,半径是R1,R2。

现在两个球分别以速度V1,V2开始做直线运动(V1,V2也是三个分量的向量)

写一个小程序,给定(输入)L1,L2,R1,R2,V1,V2

基础题判断两个球是否相撞。

扩展题:附加给定两个球的质量M1,M2。

如果相撞,求出相撞后两个球的速度V3,V4。

(假设这个空间没有任何阻力,摩擦,并且球碰撞是弹性碰撞,也就是碰撞不损失能量)

这里给出一组简单的数据,可供测试:(基础题)

L1 = (10,0,0)

L2 = (0,10,0)

V1 = (-1,0,0)

V2 = (0,-1,0)

得出的碰撞时刻是10-sqrt(2)

踢馆帝
大家积极性不高哇
展开Biu

大家积极性不高哇,先给出解答吧

[查看全文]
轻舟过
附加题只要求碰撞时刻两球心连线方向的动量守
展开Biu

想了下,附加题只要求碰撞时刻两球心连线方向的动量守恒和能量守恒就行

[查看全文]
踢馆帝
面瘫大明神
展开Biu

面瘫大明神 发表于 2012-6-24 17:23

这是数学问题吧

是什么问题不重要,重要的是解决问题

[查看全文]
面瘫大明神
这是数学问题吧
展开Biu

@@16!!这是数学问题吧

[查看全文]
LonghronShen
本帖最后由
展开Biu

本帖最后由 LonghronShen 于 2012-6-22 22:41 编辑

都占坑了啊,那我也来一个

先做出基础题。

算法:

ImportsMicrosoft.Xna.Framework
ImportsSystem.Runtime.CompilerServices
ModuleModuleMain
Public Class Ball
Public Property Center() As Vector3
Public Property R() As Double
Public Property Mass() As Double
Public ReadOnly Property IsLegal() As Boolean
Get
With Me.Center
Dim parts = {.X,.Y, .Z}
For Each p In parts
If Single.IsInfinity(p)Or Single.IsNaN(p)Then
Return False
End If
Next
End With
Return Me.R <>0
End Get
End Property
Public Sub New()
Me.Center =Vector3.Zero
Me.R = 0
End Sub
Public Sub New(ByVal p As Vector3, ByVal r As Double, ByVal m As Double)
Me.Center = p
Me.R = r
Me.Mass = m
End Sub
Public Sub New(ByVal p As Vector4)
Me.Center = New Vector3(p.X,p.Y, p.Z)
Me.R = p.W
End Sub
Public Sub New(ByVal ParamArray p() As Double)
If p.Length >= 4 Then
Me.Center = New Vector3(p(0), p(1), p(2))
Me.R = p(3)
If p.Length >=5 Then
Me.Mass =p(4)
End If
Else
Throw New ArgumentException("Too few arguments for Ball contructor.")
End If
End Sub
End Class
Public Function CreateVector4FromString(ByVal s As String, ByVal ParamArray sp() As String) As Vector4
Dim parts = s.Split(sp,StringSplitOptions.RemoveEmptyEntries)
If parts.Length >= 4 Then
Return New Vector4(parts(0), parts(1), parts(2), parts(3))
Else
Throw New ArgumentException("Wrong format for Vector4.")
End If
End Function
Public Function CreateVector3FromString(ByVal s As String, ByVal ParamArray sp() As String) As Vector3
Dim parts = s.Split(sp,StringSplitOptions.RemoveEmptyEntries)
If parts.Length >= 3 Then
Return New Vector3(parts(0), parts(1), parts(2))
Else
Throw New ArgumentException("Wrong format for Vector3.")
End If
End Function
Public Function Root(ByVal f As Func(Of Double, Double), ByVal f_ As Func(Of Double, Double), Optional ByVal x As Double = 0) As Double
If f(x) = 0 Then Return x
Dim stw As New Stopwatch()
stw.Start()
Dim x0 = x
Do
x0 = x
Dim vf = f(x0)
Dim vf_ = f_(x0)
x = x - vf/ vf_
Loop While Math.Abs((x - x0))>= 0.00001 Andstw.Elapsed.TotalSeconds < 20
stw.Stop()
If stw.Elapsed.TotalSeconds >20 Then
Throw New ArgumentException("Timeout when finding the root.")
End If
Return x
End Function
Public Function GenericCast(Of U, V)(ByVal obj As U) As V
Try
Return CType(DirectCast(obj, Object), V)
Catch ex As InvalidCastException
Return DirectCast(CompilerServices.Conversions.ChangeType(obj,GetType(V)), V)
End Try
End Function
<Extension()>_
Public Function OfType(Of U, V)(ByVal [Me] As IEnumerable(OfU)) As IEnumerable(OfV)
Dim lst As New List(Of V)
For Each item In [Me]
lst.Add(GenericCast(OfU, V)(item))
Next
Return lst
End Function
Public Sub Pause()
Console.WriteLine("Press any key to continue.")
Console.ReadKey(True)
End Sub
Sub Main()
While True
Try
Console.WriteLine("Please input data for the first ball.")
Console.WriteLine("x, y, z, r,[m]")
Dim b1 = New Ball(Console.ReadLine().Split(",").OfType(Of Double)().ToArray())
If b1.IsLegal =False Then
Throw New ArgumentException("The data for the first ball is illegal.")
End If
Console.WriteLine("Please input data for the second ball.")
Console.WriteLine("x, y, z, r,[m]")
Dim b2 = New Ball(Console.ReadLine().Split(",").OfType(Of Double)().ToArray())
If b2.IsLegal =False Then
Throw New ArgumentException("The data forthe second ball is illegal.")
End If
Console.WriteLine("Please input the velocity for the first ball.")
Console.WriteLine("Vector3: x, y,z")
Dim v1 =CreateVector3FromString(Console.ReadLine(), ",")
Console.WriteLine("Please input the velocity for the second ball.")
Console.WriteLine("Vector3: x, y,z")
Dim v2 = CreateVector3FromString(Console.ReadLine(),",")
Dim dis =
Function(t As Double)
Return Vector3.Distance(
New Vector3(b1.Center.X+ v1.X * t,
b1.Center.Y + v1.Y * t,
b1.Center.Z + v1.Z * t),
New Vector3(b2.Center.X+ v2.X * t,
b2.Center.Y + v2.Y * t,
b2.Center.Z + v2.Z * t))
End Function
Dim _dis =
Function(t As Double)
Dim g_ = Function(_t As Double)
Return 2 * (Vector3.Distance(v1,v2) ^ 2) * _t +
2 * ((b1.Center.X- b2.Center.X) * (v1.X - v2.X) +
(b1.Center.Y- b2.Center.Y) * (v1.Y - v2.Y) +
(b1.Center.Z- b2.Center.Z) * (v1.Z - v2.Z))
End Function
Return 0.5 * Math.Pow(dis(t)^ 2, -0.5) * g_(t)
End Function
Dim time = Root(dis,_dis)
If dis(time)<= Math.Abs(b1.R + b2.R) Then
Dim oldColor =Console.ForegroundColor
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("The two balls will collide with each other.")
Console.ForegroundColor = oldColor
Else
Console.WriteLine("The two balls will not collide with eachother.")
End If
Call Pause()
Catch ex As Exception
Console.WriteLine(ex.Message& vbCrLf & ex.StackTrace.ToString())
Call Pause()
Console.Clear()
Continue While
End Try
End While
End Sub
End Module

[查看全文]
踢馆帝
话说赶紧做题
展开Biu

ぃ野花のAriosモ 发表于 2012-6-21 22:41

好头像

好眼力

喂喂喂,话说赶紧做题

[查看全文]
秋声赋
本帖最后由
展开Biu

本帖最后由 秋声赋 于 2012-6-22 00:38 编辑

没仔细检查

先放在这

就说思路 代码也没仔细看

先求相对速度和距离

因为没时间,所以规范化向量

以相对速度做一个坐标轴(原点在一个球的球心)

这时 2个球在该平面(相对速度向量过新坐标系原点的正交补)的投影就是2个圆了

然后就是求这两个球心在这个平面上的距离

想想下很容易看出 相对速度,相对距离,投影平面圆心的距离是一个三角形

求出投影平面的圆心距离

然后就是2D的判断圆是否相交

[mw_shl_code=csharp,true]

bool IsPeng=false;

Vector3D L1 = new Vector3D(1, 1, 1);

Vector3D L2 = new Vector3D(1, 1, 1);

double R1 = 1;

double R2 = 1;

Vector3D V1 = new Vector3D(1, 1, 1);

Vector3D V2 = new Vector3D(1, 1, 1);

Vector3D RV = V2 - V1;

Vector3D RX = L2 - L1;

RV.Normalize();

RX.Normalize();

if ((RX - RV).Length > (RX + RV).Length) return;

double RC = Math.Sqrt(RX.LengthSquared - RV.LengthSquared);

if (RC > R1 + R2) IsPeng = false;

else IsPeng = true;

[/mw_shl_code]

[查看全文]
ぃ野花のAriosモ