C# 从用户输入(通过鼠标)将ILPoints添加到ILPlotCube
我不熟悉数字,我对一些简单的活动有问题。我想在用户鼠标输入时向绘图添加点。用户单击了绘图上的示例点5,4,该点必须在此位置绘制。 我在将鼠标位置转换为绘图坐标时遇到了一些问题,我在这里找到了解决方案,但这对我来说并不理想。我所做的:C# 从用户输入(通过鼠标)将ILPoints添加到ILPlotCube,c#,plot,ilnumerics,C#,Plot,Ilnumerics,我不熟悉数字,我对一些简单的活动有问题。我想在用户鼠标输入时向绘图添加点。用户单击了绘图上的示例点5,4,该点必须在此位置绘制。 我在将鼠标位置转换为绘图坐标时遇到了一些问题,我在这里找到了解决方案,但这对我来说并不理想。我所做的: private void ilpGraph_Load(object sender, EventArgs e) { var scene = new ILScene(); var plotCube = scene.Add(
private void ilpGraph_Load(object sender, EventArgs e)
{
var scene = new ILScene();
var plotCube = scene.Add(
new ILPlotCube()
{
new PointsGroup()
});
plotCube.Limits.Set(new Vector3(0, 0, 0), new Vector3(10, 10, 0));
plotCube.MouseClick += plotCube_MouseClick;
ilpGraph.Scene = scene;
}
void plotCube_MouseClick(object sender, ILMouseEventArgs e)
{
var pos = new Vector3(0, 0, 0);
ILGroup plotcubeGroup = sender as ILGroup;
ILGroup group = plotcubeGroup.Children.Where(item => item.Tag == "PointsGroup").First() as ILGroup;
if (group != null)
{
// walk up to the next camera node
Matrix4 trans = group.Transform;
while (!(group is ILCamera) && group != null)
{
group = group.Parent;
// collect all nodes on the path up
trans = group.Transform * trans;
}
if (group != null && (group is ILCamera))
{
// convert args.LocationF to world coords
// The Z coord is not provided by the mouse! -> choose arbitrary value
pos = new Vector3(e.LocationF.X * 2 - 1, e.LocationF.Y * -2 + 1, 0);
// invert the matrix.
trans = Matrix4.Invert(trans);
// trans now converts from the world coord system (at the camera) to
// the local coord system in the 'target' group node (surface).
// In order to transform the mouse (viewport) position, we
// left multiply the transformation matrix.
pos = trans * pos;
}
}
// it's for testing now
var pg = (sender as ILPlotCube).Plots.Children[0];
var t = ((PointsGroup)pg).Points.Positions;
int dc = t.DataCount;
t.Update(dc, 1, new[] { pos.X, pos.Y, 0.0f });
e.Refresh = true;
}
public class PointsGroup : ILGroup
{
private ILPoints _points;
public PointsGroup()
: base((object)"PointsGroup", new Vector3?(), 0.0, new Vector3?(), new Vector3?(), new RenderTarget?())
{
}
internal PointsGroup(PointsGroup source) : base((ILGroup) source)
{
}
public ILPoints Points
{
get
{
if (_points == null)
return new ILPoints();
return _points;
}
set { _points = value; }
}
protected override ILNode CreateSynchedCopy(ILNode source)
{
return (ILNode)new PointsGroup();
}
public override ILNode Copy()
{
return (ILNode)new PointsGroup(this);
}
public override ILNode Synchronize(ILNode copy, ILSyncParams syncParams)
{
return base.Synchronize(copy, syncParams);
}
}
问题是:
如果我不定义PointsGroup类,我就无法在绘图上定位鼠标坐标(ILPoints不是ILGroup,我不知道如何做得不同)。
点组坐标计算良好,但坐标不会被记住,也不会显示在绘图上。我知道为什么会这样,但我真的不知道如何解决这个问题。
如何扩展我的PointsGroup类以记住我的值并显示在绘图上,或者如何修改坐标计算以处理ILPoints对象?
有人能帮我吗?请您是否尝试简单地将点放入平面组中?没有创建独特的pointsgroup类?感谢您的回复。是的,几天前我就这样解决了我的问题。