Sol*_*ong 1 c# system.drawing nullable
我正在使用C#进行格式化.现在我想为PointF返回一个null
/// <summary>
/// Project the point according to camera position.
/// </summary>
/// <param name="p">Point position, in WC</param>
/// <returns>pixel position</returns>
public PointF Project(MCvPoint3D32f p)
{
//return new PointF(p.x, p.y);
// Extend p
Matrix<double> p_ex = new Matrix<double>(3, 1);
p_ex[0, 0] = p.x;
p_ex[1, 0] = p.y;
p_ex[2, 0] = p.z;
var rst = HomographMatrix * p_ex;
rst[0, 0] = rst[0,0] / rst[2, 0] * focalLength * scale;
rst[1, 0] = rst[1, 0] / rst[2, 0] * focalLength * scale;
return new PointF((float)rst[0, 0], (float)rst[1, 0]);
}
Run Code Online (Sandbox Code Playgroud)
如果该点超出我相机的FoV,我想在该点返回'null'.但实际上PointF不是"可空"的,那么是否有一种直接的方法可以为该点返回'null'?
我不想继承原生的PointF.
在某些方面,这取决于您的要求.例如,如果您需要存储大量的点(因此空间是一个因素),您可以使用某种类型的标记值 - 也许:
static readonly PointF NilPoint = new PointF(float.NaN, float.NaN);
Run Code Online (Sandbox Code Playgroud)
然后使用辅助方法检查该运行时:
public static bool IsNil(this PointF value)
{
return float.IsNaN(value.X) || float.IsNaN(value.Y);
}
Run Code Online (Sandbox Code Playgroud)
所以你可以检查:
if(!point.IsNil()) {
// etc
}
Run Code Online (Sandbox Code Playgroud)
但是,更简单的选择(带有轻微的空间开销)就是使用Nullable<T>,即
PointF? pt1 = null; // fine
PointF? pt2 = new PointF(123,456); // also fine
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2135 次 |
| 最近记录: |