使用偏移将点绘制到画布上?
我有一个 Point 变量数组。当使用 Graphics.DrawLine 绘制时,它们会创建预期的图像。我的问题是 0,0 实际上是图像的中心(不是预期的画布左上角。点中的 X 和 Y 坐标可以包含负数。
当我尝试将其绘制到我的 Image 时
,当然,我得到了整个图像的 1/4,因为其余部分是在画布边界之外绘制的,我如何将该绘图正确地居中到我的画布上
?我知道 0,0 在哪里(宽度/2,高度/2),
我想我可以翻译每个点,但这似乎很难做到这一点。
I have an array of Point
variables. When drawn using Graphics.DrawLine
, they create the expected image. My problem is that 0,0 is actually the center of the image (not the top left of my canvas as expected. My X and Y coordinates in the Points can contain negative numbers.
When I try to draw this to my Image
, of course I get 1/4 of the total image as the remainder is drawn outside the bounds of my canvas. How do I center this drawing correctly onto my canvas?
I know the dimensions of the image I want to draw. I know where 0,0 is (width / 2, height / 2).
I suppose I can translate each and every single Point
, but that seems like the hard way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在绘图处理程序期间设置转换,TranslateTransform() 可以为您映射坐标。
Graphics.TranslateTransform @ MSDN
或者,通过添加一半来映射坐标每个坐标所需查看区域的宽度和高度的一半。
此外,您可能需要缩放坐标。您可以使用 Graphics.ScaleTransform 来执行此操作。
Graphics.ScaleTransform @ MSDN
如果您不这样做如果不想使用它,那么您应该将 X 坐标除以您希望拉伸宽度的百分比量,并将 Y 坐标除以您希望拉伸高度的百分比量。 1 代表 100%,1.2 代表 120%,0.8 代表 80%,等等。
TranslateTransform() can map coordinates for you if you setup a transformation during your drawing handlers.
Graphics.TranslateTransform @ MSDN
Or, map your coordinates by adding half the width and half the height of the desired viewing area to each coordinate.
Also, you may need to scale your coordinates. You may use Graphics.ScaleTransform to do this.
Graphics.ScaleTransform @ MSDN
If you don't wish to use this, then you should divide X coordinates by the percent amount you wish to stretch the width, and divide Y coordinates by the percent amount you wish to stretch the height. This gives us 1 for 100%, 1.2 for 120%, 0.8 for 80%, etc.
欢迎使用 Windows 版本的笛卡尔平面。你最后的说法是正确的。你必须抵消每一个点。您可以给自己的唯一真正帮助是使偏移逻辑成为一个单独的方法来清理您的主要绘图代码。
Welcome to the Windows' version of the Cartessian Plane. Your last statement is correct. You do have to offset each and every point. The only real help you can give yourself is to make the offset logic a separate method to clean up your main drawing code.
创建数组时,为每个 x 值添加一个等于宽度一半的偏移量,并为每个 y 值添加一个等于高度一半的偏移量。这样,当绘制点时,它们就位于预期的位置。
When creating the array, add an offset to each x value equal to half of the width and an offset to y equal to half of the height. That way when the points are drawn, they're in the expected position.