在墨水画布上绘制波浪线(正弦波?)
大家好,
我正在寻找一种在墨水画布上绘制波浪线的解决方案.因此,每当我在画布上绘制某些东西时,都应将其渲染为波浪线.我找到了一种使用sineFunction在Winforms中执行此操作的方法(但仅适用于直线!).我将起点和终点传递给函数并将其绘制在表单上.
范例绘制曲线的代码:
Hi all,
i''m looking for a solution on how to draw a wavy line on inkcanvas. So whenever i draw something on inkcanvas, it should be rendered to a wavy line. I found a way on how to do it in Winforms using sineFunction (but only for straight Lines!). I pass the start and end point to the function and draw it on the form.
Exmpl. code of drawing curve:
<pre lang="vb">
Dim t As Graphics = CreateGraphics()
t.DrawCurve(p, sine(A, B)) '' A,B start and endpoints
Public Function sine(ByVal start As Point, ByVal ende As Point) As Point()
Dim sidea As Integer = ende.X - start.X
Dim sideb As Integer = ende.Y - start.Y
Dim hypot As Double = CSng(Sqrt((sidea ^ 2) + (sideb ^ 2)))
Dim angle As Double = CSng(Atan2(sideb, sidea))
Dim points As Point() = New Point(10) {}
Dim c As Integer = 0, n As Integer = 10
While c <= 10
points(c) = New Point(CInt(hypot / 10 * c), n)
n = -n
c += 1
End While
Dim mx As New Drawing2D.Matrix()
mx.Rotate(CSng(angle / Math.PI * 180))
mx.Translate(A.X, A.Y, Drawing2D.MatrixOrder.Append)
mx.TransformPoints(points)
Return (points)
End Function
我找到了动态渲染器绘制椭圆,矩形等的方法,但没有找到波浪线的方法.
提前寻求任何帮助
I''v found ways for a dynamic renderer to draw ellips, rects and so on, but not for wavy line.
thx in advance for any help
此 [ ^ ]应该可以帮助您.
This[^] should surely help you out.
Abhinav的链接很好.但是,该解决方案的简化版本是...
Xaml ..
Abhinav''s link is good. However the simplified version of that solution is...
Xaml..
<Polyline Name="polyline1" Stroke="Red"
StrokeThickness="2"/>
代码..
code..
for (int i = 0; i < 70; i++)
{
double x = i * Math.PI;
double y = 40 + 30 * Math.Sin(x/10);
polyline1.Points.Add(new Point(x, y));
}
其中40只是图形的转换因子.振幅为30.这只是为了说明如何使用trig而不是您只复制代码来形成正弦曲线.
改进的答案
---------------------
Where the 40 is simply a translation factor for the graphic. 30 is the amplitude. This is just to explain how a sinusoidal formed using trig instead of you just copy cat the code.
Improved Answer
---------------------
If need to draw a wavy polyline on a canvas using a start and end point then this function may help
public void drawWawyPolyline(Point start, Point end)
{
double x=0;
double y=0;
double distance = Math.Sqrt(Math.Pow((start.X - end.X), 2) + Math.Pow((start.Y - end.Y), 2));
Point refPoint = new Point(start.X + distance, start.Y);
double endX = (refPoint.X / Math.PI) + 1;
double angle1 = rad2deg(Math.Acos(Math.Abs((end.X - start.X)) / distance));
double angle2 = rad2deg(Math.Asin(Math.Abs((end.Y - start.Y)) / distance));
double angle = (angle1 + angle2) / 2;
for (double startX = start.X / Math.PI; startX <= endX; startX++)
{
x = startX * Math.PI;
y = start.Y + 10 * Math.Sin(x / 10);
polyline1.Points.Add(new Point(x, y));
}
RotateTransform trans = new RotateTransform(angle, start.X, start.Y);
polyline1.RenderTransform = trans;
}
private double deg2rad(double deg)
{
return Math.PI * deg / 180;
}
private double rad2deg(double rad)
{
return rad * (180 / Math.PI);
}