4 changed files with 77 additions and 0 deletions
@ -0,0 +1,46 @@ |
|||||
|
using System; |
||||
|
using Avalonia; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Media; |
||||
|
using Avalonia.Rendering.SceneGraph; |
||||
|
using Avalonia.Threading; |
||||
|
|
||||
|
namespace RenderDemo.Controls |
||||
|
{ |
||||
|
public class LineBoundsDemoControl : Control |
||||
|
{ |
||||
|
private double angle = Math.PI / 8; |
||||
|
|
||||
|
public static double CalculateOppSide(double angle, double hyp) |
||||
|
{ |
||||
|
return Math.Sin(angle) * hyp; |
||||
|
} |
||||
|
|
||||
|
public static double CalculateAdjSide(double angle, double hyp) |
||||
|
{ |
||||
|
return Math.Cos(angle) * hyp; |
||||
|
} |
||||
|
|
||||
|
public override void Render(DrawingContext drawingContext) |
||||
|
{ |
||||
|
var lineLength = Math.Sqrt((100 * 100) + (100 * 100)); |
||||
|
|
||||
|
var diffX = CalculateAdjSide(angle, lineLength); |
||||
|
var diffY = CalculateOppSide(angle, lineLength); |
||||
|
|
||||
|
|
||||
|
var p1 = new Point(200, 200); |
||||
|
var p2 = new Point(p1.X + diffX, p1.Y + diffY); |
||||
|
|
||||
|
var pen = new Pen(Brushes.Green, 20, lineCap: PenLineCap.Square); |
||||
|
var boundPen = new Pen(Brushes.Black); |
||||
|
|
||||
|
drawingContext.DrawLine(pen, p1, p2); |
||||
|
|
||||
|
drawingContext.DrawRectangle(boundPen, LineBoundsHelper.CalculateBounds(p1, p2, pen)); |
||||
|
|
||||
|
angle += Math.PI / 360; |
||||
|
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
<UserControl xmlns="https://github.com/avaloniaui" |
||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" |
||||
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" |
||||
|
xmlns:controls="clr-namespace:RenderDemo.Controls" |
||||
|
x:Class="RenderDemo.Pages.LineBoundsDemo"> |
||||
|
<controls:LineBoundsDemoControl /> |
||||
|
</UserControl> |
||||
@ -0,0 +1,19 @@ |
|||||
|
using Avalonia; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Markup.Xaml; |
||||
|
|
||||
|
namespace RenderDemo.Pages |
||||
|
{ |
||||
|
public class LineBoundsDemo : UserControl |
||||
|
{ |
||||
|
public LineBoundsDemo() |
||||
|
{ |
||||
|
this.InitializeComponent(); |
||||
|
} |
||||
|
|
||||
|
private void InitializeComponent() |
||||
|
{ |
||||
|
AvaloniaXamlLoader.Load(this); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue