comparison MetroWpf/MetroWpf.Xaml/Converters/AngleToCoordinateConverter.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
comparison
equal deleted inserted replaced
14:741981715d94 15:060f02cd4591
1 using System;
2 using System.Diagnostics.Contracts;
3 using System.Globalization;
4 using System.Windows;
5 using System.Windows.Data;
6
7 namespace MetroWpf.Xaml.Converters
8 {
9 public sealed class AngleToCoordinateConverter : IMultiValueConverter
10 {
11 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
12 {
13 if (values == null || values.Length < 4)
14 {
15 return DependencyProperty.UnsetValue;
16 }
17
18 var fallbackValue = values[0];
19 if (!(values[1] is double) || !(values[2] is double) || !(values[3] is double))
20 {
21 return fallbackValue;
22 }
23 var angle = (double)values[1];
24 if (angle < 0.0)
25 {
26 return fallbackValue;
27 }
28 var areaWidth = (double)values[2];
29 var areaHeight = (double)values[3];
30
31 var width = values.Length > 4 ? (values[4] is double ? (double)values[4] : 0.0) : 0.0;
32 var height = values.Length > 5 ? (values[5] is double ? (double)values[5] : 0.0) : 0.0;
33 var radiusXCoordinate = values.Length > 6 ? (values[5] is double ? (double)values[5] : 0.0) : areaWidth / 2;
34 var radiusYCoordinate = values.Length > 7 ? (values[6] is double ? (double)values[6] : 0.0) : areaHeight / 2;
35
36 var length = Math.Max(width, height);
37 var radius = Math.Min(areaWidth / 2, areaHeight / 2) - length;
38
39 switch (parameter as string)
40 {
41 case "X":
42 case "x":
43 var x = radiusXCoordinate + radius * Math.Cos(angle * Math.PI / 180);
44 return x;
45 case "Y":
46 case "y":
47 var y = radiusYCoordinate + radius * Math.Sin(angle * Math.PI / 180);
48 return y;
49 default:
50 return fallbackValue;
51 }
52 }
53
54 public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
55 {
56 Contract.Ensures(false);
57 throw new NotSupportedException();
58 }
59 }
60 } ;