Skip to content

Commit

Permalink
Rework display of the Canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
Simi23 committed Aug 10, 2021
1 parent bea3c3a commit 90b2fcc
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 66 deletions.
9 changes: 6 additions & 3 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Trigonometrics"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
Title="MainWindow" Height="546" Width="983">
<Grid>
<TextBox x:Name="angleInput" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="angleInput_TextChanged"/>
<Label x:Name="lb_sin" Content="Szinusz:" HorizontalAlignment="Left" Margin="139,9,0,0" VerticalAlignment="Top"/>
Expand All @@ -16,7 +16,10 @@
<Label x:Name="lb_v_cos" Content="" HorizontalAlignment="Left" Margin="232,40,0,0" VerticalAlignment="Top"/>
<Label x:Name="lb_v_tg" Content="" HorizontalAlignment="Left" Margin="232,71,0,0" VerticalAlignment="Top"/>
<Label x:Name="lb_v_ctg" Content="" HorizontalAlignment="Left" Margin="232,102,0,0" VerticalAlignment="Top"/>
<Canvas x:Name="mainCanvas" HorizontalAlignment="Left" Height="370" Margin="387,10,0,0" VerticalAlignment="Top" Width="370"/>

<Border HorizontalAlignment="Left" VerticalAlignment="Top" Margin="387,10,0,0" Height="400" Width="400" BorderBrush="Gray" BorderThickness="2">
<StackPanel HorizontalAlignment="Left" Height="400" Margin="0,0,0,0" VerticalAlignment="Top" Width="400" ClipToBounds="True">
<Canvas x:Name="mainCanvas" HorizontalAlignment="Left" Height="400" Margin="0,0,0,0" VerticalAlignment="Top" Width="400"/>
</StackPanel>
</Border>
</Grid>
</Window>
155 changes: 92 additions & 63 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private static double CenterX = 200;
private static double CenterY = 200;
private static double ZoomFactor = 1;

private void GenerateCanvasDrawing(double alpha, bool showTan, bool tanRight) {
private void GenerateCanvasDrawing(double alpha, bool showTanHelp, bool tanRight/*, bool showCot, bool cotUp*/) {
mainCanvas.Children.Clear();

// Main Circle
Expand All @@ -35,29 +38,31 @@ private void GenerateCanvasDrawing(double alpha, bool showTan, bool tanRight) {
};
mainCanvas.Children.Add(circle);
Canvas.SetZIndex(circle, 0);
Canvas.SetLeft(circle, CenterX - 100);
Canvas.SetTop(circle, CenterY - 100);

circle.MouseEnter += Circle_MouseEnter;
circle.MouseLeave += Circle_MouseLeave;

// Base Line
Line baseLine = new Line() {
X1 = 100,
Y1 = 100,
X1 = CenterX,
Y1 = CenterY,

X2 = 200,
Y2 = 100,
X2 = CenterX + 100,
Y2 = CenterY,
Stroke = Brushes.Blue,
StrokeThickness = 3
};
mainCanvas.Children.Add(baseLine);

// Secondary Line
Line secLine = new Line() {
X1 = 100,
Y1 = 100,
X1 = CenterX,
Y1 = CenterY,

X2 = 100 + Math.Cos(alpha) * 100,
Y2 = 100 - Math.Sin(alpha) * 100,
X2 = CenterX + Math.Cos(alpha) * 100,
Y2 = CenterY - Math.Sin(alpha) * 100,
Stroke = Brushes.Blue,
StrokeThickness = 3
};
Expand All @@ -66,10 +71,10 @@ private void GenerateCanvasDrawing(double alpha, bool showTan, bool tanRight) {
// Coord line x
Line coordLineX = new Line() {
X1 = 0,
Y1 = 100,
Y1 = CenterY,

X2 = 200,
Y2 = 100,
X2 = mainCanvas.ActualWidth,
Y2 = CenterY,
Stroke = Brushes.Gray,
StrokeDashArray = new DoubleCollection() { 6, 1 },
StrokeThickness = 2
Expand All @@ -79,11 +84,11 @@ private void GenerateCanvasDrawing(double alpha, bool showTan, bool tanRight) {

// Coord line y
Line coordLineY = new Line() {
X1 = 100,
X1 = CenterX,
Y1 = 0,

X2 = 100,
Y2 = 200,
X2 = CenterX,
Y2 = mainCanvas.ActualHeight,
Stroke = Brushes.Gray,
StrokeDashArray = new DoubleCollection() { 6, 1 },
StrokeThickness = 2
Expand All @@ -93,11 +98,11 @@ private void GenerateCanvasDrawing(double alpha, bool showTan, bool tanRight) {

// sinLine
Line sinLine = new Line() {
X1 = 100 + Math.Cos(alpha) * 100,
Y1 = 100,
X1 = CenterX + Math.Cos(alpha) * 100,
Y1 = CenterY,

X2 = 100 + Math.Cos(alpha) * 100,
Y2 = 100 - Math.Sin(alpha) * 100,
X2 = CenterX + Math.Cos(alpha) * 100,
Y2 = CenterY - Math.Sin(alpha) * 100,
Stroke = Brushes.Red,
StrokeThickness = 3
};
Expand All @@ -106,53 +111,58 @@ private void GenerateCanvasDrawing(double alpha, bool showTan, bool tanRight) {

// cosLine
Line cosLine = new Line() {
X1 = 100,
Y1 = 100 - Math.Sin(alpha) * 100,
X1 = CenterX,
Y1 = CenterY - Math.Sin(alpha) * 100,

X2 = 100 + Math.Cos(alpha) * 100,
Y2 = 100 - Math.Sin(alpha) * 100,
X2 = CenterX + Math.Cos(alpha) * 100,
Y2 = CenterY - Math.Sin(alpha) * 100,
Stroke = Brushes.SlateBlue,
StrokeThickness = 3
};
mainCanvas.Children.Add(cosLine);
Canvas.SetZIndex(cosLine, 1);

// tan line
if (showTan) {
// Main line
Brush tanBrush = Math.Tan(alpha) >= 0 ? Brushes.OrangeRed : Brushes.CornflowerBlue;
double tanX1, tanX2, tanY1, tanY2;

double tan = tanRight ? Math.Tan(alpha) : Math.Tan(alpha) * -1;

if (tanRight) {
tanX1 = 200;
tanY1 = 100;
tanX2 = 200;
tanY2 = 100 - tan * 100;
} else {
tanX1 = 0;
tanY1 = 100;
tanX2 = 0;
tanY2 = 100 - tan * 100;
}

Line tanLine = new Line() {
X1 = tanX1,
Y1 = tanY1,
// Main line
Brush tanBrush = Math.Tan(alpha) >= 0 ? Brushes.OrangeRed : Brushes.CornflowerBlue;
double tanX1, tanX2, tanY1, tanY2;

X2 = tanX2,
Y2 = tanY2,
Stroke = tanBrush,
StrokeThickness = 3
};
mainCanvas.Children.Add(tanLine);
Canvas.SetZIndex(tanLine, 1);
double tan = tanRight ? Math.Tan(alpha) : Math.Tan(alpha) * -1;

if (tanRight) {
tanX1 = CenterX + 100;
tanY1 = CenterY;
tanX2 = CenterX + 100;
tanY2 = CenterY - tan * 100;
} else {
tanX1 = CenterX - 100;
tanY1 = CenterY;
tanX2 = CenterX - 100;
tanY2 = CenterY - tan * 100;
}

if (Math.Abs(ConvertToDegrees(alpha)) % 180 - 90 == 0)
{
tanY2 = Math.Min(mainCanvas.ActualHeight, Math.Max(0, tanY2));
}

Line tanLine = new Line() {
X1 = tanX1,
Y1 = tanY1,

X2 = tanX2,
Y2 = tanY2,
Stroke = tanBrush,
StrokeThickness = 3
};
mainCanvas.Children.Add(tanLine);
Canvas.SetZIndex(tanLine, 1);

if (showTanHelp) {
// Helper line
Line tanHelper = new Line() {
X1 = 100,
Y1 = 100,
X1 = CenterX,
Y1 = CenterY,

X2 = tanX2,
Y2 = tanY2,
Expand Down Expand Up @@ -205,7 +215,7 @@ private void angleInput_TextChanged(object sender, TextChangedEventArgs e) {
lb_v_tg.Content = Math.Round(Math.Tan(rad), roundDecimals);
lb_v_ctg.Content = Math.Round(1 / Math.Tan(rad), roundDecimals);

GenerateCanvasDrawing(rad, DetermineShowTan(angle), DetermineRightSide(angle));
GenerateCanvasDrawing(rad, DetermineShowTanHelper(angle), DetermineTanRight(angle));

} else if (angleInput.Text.Length == 0) {
angleInput.BorderBrush = System.Windows.Media.Brushes.LightBlue;
Expand All @@ -231,28 +241,47 @@ public double ConvertToRadians(double angle) {
return (Math.PI / 180) * angle;
}

public bool DetermineRightSide(double angle) {
if (Math.Abs(angle) % 90 == 0)
return false;
public double ConvertToDegrees(double radian)
{
return radian / (Math.PI / 180);
}

public bool DetermineTanRight(double angle) {
double tempAngle = angle % 360;
if(tempAngle < 0) {
tempAngle += 360;
}

if (tempAngle < 90) {
if (tempAngle <= 90)
{
return true;
} else if (tempAngle < 180) {
}
else if (tempAngle < 180)
{
return false;
} else if (tempAngle < 270) {
}
else if (tempAngle <= 270)
{
return false;
} else {
}
else
{
return true;
}
}

public bool DetermineShowTan(double angle) {
public bool DetermineShowTanHelper(double angle) {
return Math.Abs(angle) % 90 != 0;
}

public bool DetermineCotUp(double angle)
{
return false;
}

public bool DetermineShowCot(double angle)
{
return false;
}
}
}

0 comments on commit 90b2fcc

Please sign in to comment.