WinForms를 사용하여 시뮬레이션을위한 시각화 프로그램을 작성하고 있습니다. 시각화에는 그리드를 따라 움직이는 다양한 객체가 포함됩니다.크기 조정/변환 된 그래픽 객체를 가져올 수 있습니까?
지금까지 필자는 Panel을 확장하고 Paint 이벤트 중에 Graphics 클래스를 사용하여 사용자 지정 그리기를 수행하는 사용자 지정 컨트롤을 사용하고 있습니다. 그러나 한 가지 염려는 내가 계속 그리드 좌표에서 컨트롤로 물건을 확장해야한다는 것입니다 .DisplayRectangle 좌표 (즉, 그리드에서 2 개의 셀을 차지하는 객체는 2 * (control.DisplayRectangle.Width/horizontalGridWidth) 픽셀을 그릴 때).
그리드 좌표에서 내 그림을 표현할 수 있고 물리적 좌표로 자동 매핑되도록 그래픽 객체에서 이러한 변환을 수행하는 방법이 있습니까?
실제로 매트릭스가 핵심 이었음이 밝혀졌습니다 (수락 된 답변 참조). 다음은 작동하는 코드입니다.
public SimulationPanel() {
this.DoubleBuffered = true;
this.SizeChanged += (o, e) => this.Invalidate();
this.Paint += this.PaintPanel;
}
private void Paint(object sender, PaintEventArgs e) {
e.Graphics.Clear(Color.Black);
var fromRectangle = GetSimulationWorldCoordinates();
var toRectangle = ScaleToFit(fromRectangle, this.DisplayRectangle);
using (var matrix = new Matrix(
fromRectangle,
new[] {
toRectangle.Location,
new Point(toRectangle.Right, toRectangle.Top),
new Point(toRectangle.Left, toRectangle.Bottom),
}))
{
// draw the simulation stuff here using simulation coordinates
e.Graphics.Transform = matrix;
e.Graphics.FillRectangle(Brushes.LightBlue, toRectangle);
e.Graphics.DrawLine(Pens.Red, toRectangle.Location, new Point(toRectangle.Right, toRectangle.Bottom));
}
}
Graphics.ScaleTransform() 만 사용하면됩니다. ScaleTransform의 두 번째 인수에 음수 값이있는 TranslateTransform()을 사용하면 좌표계를 반전하고 왼쪽 하단 구석에서 원점을 얻을 수 있습니다. –