더 나은 솔루션이 당신의 경로를 저장하는 하나 이상의 System.Windows.Media.Geometry 객체를 사용하는 것,
이 기하하는 펜으로 그릴 수 등 지적 두께를 조정할 때 Transform 속성을 사용하는 것이 더 유연합니다.
변환을 사용하면 시각화가 아닌 기하학적 표현의 실제 좌표를 "확대/축소"할 수 있습니다. 그리면 렌더링 할 때 렌더링 변환을 할 필요가 없습니다. fromPosition 사각형이 변환되지 않은 경계를 포함해야하고, toPosition 사각형이 변환 된 경계를 포함해야하는 위치
public static Matrix TransformShape(Rect fromPosition, Rect toPosition, bool flipVertical) {
Matrix translateThenScale = Matrix.Identity;
//we first translate to origin since that's just easier
translateThenScale.Translate(-fromPosition.X, -fromPosition.Y);
//now we scale the graph to the appropriate dimensions
translateThenScale.Scale(toPosition.Width/fromPosition.Width, toPosition.Height/fromPosition.Height);
//then we flip the graph vertically around the viewport middle since in our graph positive is up, not down.
if (flipVertical)
translateThenScale.ScaleAt(1.0, -1.0, 0.0, toPosition.Height/2.0);
//now we push the graph to the right spot, which will usually simply be 0,0.
translateThenScale.Translate(toPosition.X, toPosition.Y);
return translateThenScale;
}
:
내가 좋아하는 다음 코드를 사용하여 변환을 계산합니다. 이것은 또한 X와 Y를 개별적으로 스케일링 할 수있게 해주므로 플로팅에 종종 필요합니다.
그것은 당신의 형상의 경계를 계산하기 쉽다 :
Geometry graphGeom;
//[...]
//the bounds are modified by the transform, so we want no transform!
graphGeom.Transform = Transform.Identity;
Rect graphBounds = graphGeom.Bounds;
//then set the transform again
//or, if the transform is axis-aligned, the following _should_ work:
Rect graphBoundsAlt = graphGeom.Transform.Inverse.TransformBounds(graphGeom.Bounds);
그리고 물론 WPF는 당신이 필요 있어야로 렌더링 할 필요가 경계하는 당신에게 말할 수 있습니다. 함께 퍼팅,이 솔루션을 사용의 장점은 당신이 무료로 사용할 수있어, RenderTransforms로 엉망 필요가 없다는 것입니다
public void RecomputeTransform(Rect targetRect, bool flipVertical) {
graphGeom.Transform = Transform.Identity;
Rect graphBounds = graphGeom.Bounds;
Matrix transMat = TransformShape(graphBounds,targetRect,flipVertical);
graphGeom.Transform = new MatrixTransform(transMat);
}
처럼 뭔가를 할 수는 전단 및/또는 스케일 X 및 Y 변환 펜을 불투명 한 오브젝트로 취급 할 수 있습니다 (즉, UI에서 사용자 정의하기가 더 쉽습니다 - 펜 너비 또는 기타 등을 선택하면 더 이상 수정하지 않아도됩니다).
x와 y 비율은 균일하지만 비늘이 같지 않은 경우에는 문제가 없습니다. – Klay