매트릭스를 사용하여 그래픽스 패스를 확대했습니다. 어떻게 새로운 경로가 그것의 측면이 아닌 더 작은 경로 위에 정확하게 놓 이도록 설정할 수 있습니까? 직사각형이있는 팽창과 같습니다.매트릭스 및 그래픽 경로
0
A
답변
1
GraphicsPath의 핵심 부분은 이러한 점을 혼합하는 방법 (긴장이 적용될 때)을 지정하는 제어점이있는 여러 점입니다. 행렬 연산을 그래픽 경로에 적용하면 행렬에서 수행중인 연산을 기반으로 모든 점을 업데이트합니다. 내가 제대로 질문을 이해하면 마음에, 당신은 그래픽 경로의 경계를 얻어서 시작 하심
:
var graphicsBounds = myPath.GetBounds();
거기에서 당신이 중심되는 경계를 상쇄 매트릭스를 생성 할 수 있습니다 (0 , 0), 축척 (x/y 방향으로 축척 됨), 화면의 원래 위치로 다시 오프셋됩니다. 이렇게하면 원래 경계의 중심에서 대칭으로 경로가 확장됩니다. 그 행렬 코드는 다음과 같습니다 :
Matrix myMatrix = new Matrix();
myMatrix.TranslateTransform(graphicsBounds.X + graphicsBounds.Width/2.0f, graphicsBounds.Y + graphicsBounds.Height/2.0f);
myMatrix.Scale(scaleX, scaleY);
myMatrix.TranslateTransform(-graphicsBounds.X - graphicsBounds.Width/2.0f, -graphicsBounds.Y - graphicsBounds.Height/2.0f);
역순으로 행렬 순서 (기본값)가 적용됩니다. 따라서 이러한 행렬 연산을 아래에서 위로 읽어야합니다. 이것은 정확하지 않을 수도 있지만 적어도 도움이 될 것입니다. 행운을 빕니다!
0
나는 코드가 pictureBox1의 Paint 이벤트에 넣고이 경우
변환 사용하여 boundning의 rectange에 그래픽에 맞는 방법이 있습니다. GraphicsPath 및 picturebox 경계에 포함 된 그래픽 경계를 사용자 함수 GetMatrixFitRectInBounds()에 보내면 그리기 전에 그림 상자의 Graphics.Transform 속성에 적용되도록 매트릭스가 반환됩니다.이 변환하는 매트릭스를 사용하는 일반적인 개념을 보여 : - 관련 연산 거기 어쩌면 많은 때문에
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
//GraphicsPath is a great to store for graphics if repainted frequently
var myPath = new GraphicsPath();
//Add simple ellipse
myPath.AddEllipse(new Rectangle(0, 0, 50, 50));
//Get transform to fit a rectange within a bounding rectange
var T = GetMatrixFitRectInBounds(myPath.GetBounds(), new RectangleF(pictureBox1.ClientRectangle.Location, pictureBox1.ClientRectangle.Size));
//Apply transformation matrix
e.Graphics.Transform = T;
// Create a pen for the path
Pen myPen = new Pen(Color.Black, 1);
//Draw path to picturebox
e.Graphics.DrawPath(myPen, myPath);
}
//Return Matrix to scale a rectange within a rectangle
private Matrix GetMatrixFitRectInBounds(RectangleF fitrect, RectangleF boundsrect)
{
var T = new Matrix();
var bounds_center = new PointF(boundsrect.Width/2, boundsrect.Height/2);
//Set translation centerpoint
T.Translate(bounds_center.X, bounds_center.Y);
//Get smallest size to scale to fit boundsrect
float scale = Math.Min(boundsrect.Width/fitrect.Width, boundsrect.Height/fitrect.Height);
T.Scale(scale, scale);
//Move fitrect to center of boundsrect
T.Translate(bounds_center.X - fitrect.X - fitrect.Width/2f, bounds_center.Y - fitrect.Y - fitrect.Height/2f);
//Restore translation point
T.Translate(-bounds_center.X, -bounds_center.Y);
return T;
}
결과 행렬을 만들 때 kep이 될 수 있습니다. 페인트 바깥 쪽에서 경계가 바뀔 때만 생성됩니다. GraphicPath (은)는, Paint 이벤트의 외부에 작성할 수도있어, 기반이되는 데이터가 변경되었을 때에 갱신 할 수도 있습니다.
스케일링은 선 너비를 조절합니다. 원래의 펜 너비를 유지하려면 'Pen myPen = new Pen (Color.Blue, linewidth/scale)'과 같이 스케일 그래픽에 그리기 전에 펜 너비를 역 스케일링하여 취소 할 수 있습니다. – flodis