You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.2 KiB
99 lines
3.2 KiB
using SharpDX; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
using System.Windows.Media.Media3D; |
|
|
|
namespace SparkClient.Views.UserControl.ViewportData.Helper |
|
{ |
|
internal class LineCalculationHelper |
|
{ |
|
private readonly float Y = -0.01F; |
|
private Vector3 point1; |
|
private Vector3 point2; |
|
/// <summary> |
|
/// 常数a(y=ax+b) |
|
/// </summary> |
|
private float a; |
|
/// <summary> |
|
/// 常数b(y=ax+b) |
|
/// </summary> |
|
private float b; |
|
public LineCalculationHelper(Vector3 point1, Vector3 point2) { |
|
this.point1 = point1; |
|
this.point2 = point2; |
|
this.a = calA(point1, point2); |
|
this.b = calB(point1, a); |
|
} |
|
/// <summary> |
|
/// 常数a的计算 |
|
/// </summary> |
|
/// <param name="point1"></param> |
|
/// <param name="point2"></param> |
|
/// <returns></returns> |
|
private float calA(Vector3 point1, Vector3 point2) |
|
{ |
|
return (point1.Z - point2.Z) / (point1.X - point2.X); |
|
} |
|
/// <summary> |
|
/// 常数b的计算 |
|
/// </summary> |
|
/// <param name="point1"></param> |
|
/// <param name="a"></param> |
|
/// <returns></returns> |
|
private float calB(Vector3 point1, float a) |
|
{ |
|
return -(a * point1.X)+point1.Z; |
|
} |
|
|
|
public Tuple<Vector3, Vector3> calXline(float length) |
|
{ |
|
var x1 = 5; |
|
var z1 = calZ(x1); |
|
var x2 = -5; |
|
var z2 = calZ(x2); |
|
return new Tuple<Vector3, Vector3>(new Vector3(x1, Y, z1), new Vector3(x2, Y, z2)); |
|
} |
|
public Tuple<Vector3, Vector3> calYline(float length) |
|
{ |
|
var x1 = 5; |
|
var z1 = calZVertical(x1); |
|
var x2 = -5; |
|
var z2 = calZVertical(x2); |
|
return new Tuple<Vector3, Vector3>(new Vector3(x1, Y, z1), new Vector3(x2, Y, z2)); |
|
} |
|
|
|
public Tuple<Vector3, Vector3> calLineOfOffset(Tuple<Vector3, Vector3> tuple, float d) |
|
{ |
|
var point1 = tuple.Item1; |
|
var point2 = tuple.Item2; |
|
var x1 = point1.X - d * (point2.Z - point1.Z)/ |
|
(float)Math.Sqrt(Square(point2.X - point1.X) + Square(point2.Z - point1.Z)); |
|
var z1 = point1.Z + d * (point2.X - point1.X) / |
|
(float)Math.Sqrt(Square(point2.X - point1.X) + Square(point2.Z - point1.Z)); |
|
|
|
var x2 = point2.X - d * (point2.Z - point1.Z) / |
|
(float)Math.Sqrt(Square(point2.X - point1.X) + Square(point2.Z - point1.Z)); |
|
var z2 = point2.Z + d * (point2.X - point1.X) / |
|
(float)Math.Sqrt(Square(point2.X - point1.X) + Square(point2.Z - point1.Z)); |
|
return new Tuple<Vector3, Vector3>(new Vector3(x1, Y, z1), new Vector3(x2, Y, z2)); |
|
} |
|
|
|
float Square(float number) |
|
{ |
|
return number * number; |
|
} |
|
|
|
public float calZ(float x, float offset = 0) |
|
{ |
|
return a * x + b + offset; |
|
} |
|
|
|
public float calZVertical(float x, float offset = 0) |
|
{ |
|
return (-x / a) + b + offset; |
|
} |
|
} |
|
}
|
|
|