我使用SharpDX以便在.NET WinForms上绘制折线。这些折线代表钣金的轮廓。
组成PathGeometry对象,定义了折线:
'reset m_pathGeometry (the core geometry of this sheetmetal profile)
m_pathGeometry = New PathGeometry(D2DCanvas.canvas_factory2D)
' pointsRef = collection of points showing the segments & bends in this sheetmetal profile
Dim pointsRef As List(Of Vector2) = calculateSegmentsPoints()
' Add lines to m_pathGeometry using our points collection
Using sink As SimplifiedGeometrySink = m_pathGeometry.Open()
sink.BeginFigure(pointsRef.First(), FigureBegin.Filled)
sink.AddLines(pointsRef.ToArray)
sink.EndFigure(FigureEnd.Open)
sink.Close()
End Using
在绘制到屏幕上之前,我应用了一个变换(以考虑到变换和缩放):
' update m_fillTransformedGeometry based on m_pathGeometry & the current transform matrix3x2,
m_fillTransformedGeometry = New TransformedGeometry(D2DCanvas.canvas_factory2D, m_pathGeometry, Matrix3x2.Rotation(m_rotation, InsertPoint) * _transformMatrix)
' to draw the polyline
D2DCanvas.canvas_renderTarget2D.DrawGeometry(m_fillTransformedGeometry, m_fillBrush, plaatdikte * _transformMatrix.ScaleVector(1), m_strokeStyle)
折线以笔触样式绘制,圆角线连接和扁平端盖
With m_strokeStyleProps
.LineJoin = LineJoin.Round
.EndCap = CapStyle.Flat
.StartCap = CapStyle.Flat
End With
结果有点苍白,可以用中风:
第一个想法是用更深的颜色绘制同一条折线,但StrokeWidth稍微宽一些:
' First draw a wider stroke
D2DCanvas.canvas_renderTarget2D.DrawGeometry(m_fillTransformedGeometry, m_strokeBrush, (plaatdikte + 0.2) * _transformMatrix.ScaleVector(1), m_strokeStyle)
' Then draw the fill
D2DCanvas.canvas_renderTarget2D.DrawGeometry(m_fillTransformedGeometry, m_fillBrush, plaatdikte * _transformMatrix.ScaleVector(1), m_strokeStyle)
Second idea is to create a (slightly widened) PathGeometry for the stroke itself by calling the Widen() method on the original polyline-PathGeometry.
'define m_widenedPathGeometry (makes op the outline/stroke of this sheetmetal profile)
m_widenedPathGeometry = New PathGeometry(D2DCanvas.canvas_factory2D)
Using sink As SimplifiedGeometrySink = m_widenedPathGeometry.Open()
m_pathGeometry.Widen(0.2, sink)
sink.Close()
End Using
This widened geometry gets drawn (taking in account transformation) as a 'stroke' prior to drawing the 'fill' (both using the same StrokeStyle!):
' update m_transGeom based on m_widenedPathGeometry & the current transform matrix3x2,
m_transGeom = New TransformedGeometry(D2DCanvas.canvas_factory2D, m_widenedPathGeometry, Matrix3x2.Rotation(m_rotation, InsertPoint) * _transformMatrix)
' to draw the stroke of the Polyline
D2DCanvas.canvas_renderTarget2D.DrawGeometry(m_transGeom, m_strokeBrush, plaatdikte * _transformMatrix.ScaleVector(1), m_strokeStyle)
' update m_fillTransformedGeometry based on m_pathGeometry & the current transform matrix3x2,
m_fillTransformedGeometry = New TransformedGeometry(D2DCanvas.canvas_factory2D, m_pathGeometry, Matrix3x2.Rotation(m_rotation, InsertPoint) * _transformMatrix)
' to draw the fill of the PolyLine
D2DCanvas.canvas_renderTarget2D.DrawGeometry(m_fillTransformedGeometry, m_fillBrush, plaatdikte * _transformMatrix.ScaleVector(1), m_strokeStyle)
This generates an undesired effect on the End-Caps: (Either rounded, wich isn't defined in the StrokeStyle or something in between)
This Stroke/Fill - method kindof works correctly when I redefine the used StrokeStyle to have 'rounded' endcaps:
But the 'flat' end-caps is really what I'm after.
Any ideas why this is happening, or how I could tackle this differently ?
Cheers !
Found out what was wrong here!
为可能遇到类似情况的其他人写下来。
在创建加宽的几何图形时,我添加了“ FigureEnd”属性设置为“ Open”的情况,这可以防止几何图形在其自身上创建“闭环”,从而导致产生这些伪像。
m_widenedPathGeometry = New PathGeometry(D2DCanvas.canvas_factory2D)
Using sink As SimplifiedGeometrySink = m_widenedPathGeometry.Open()
m_pathGeometry.Widen(0.2, sink)
**sink.EndFigure(FigureEnd.Open)** <- adding this line leaves the stroke open!
sink.Close()
End Using
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句