Jypeli  9
The simple game programming library
Angle.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2009 University of Jyväskylä, Department of Mathematical
4  * Information Technology.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #endregion
25 
26 /*
27  * Authors: Tero Jäntti, Tomi Karppinen, Janne Nikkanen.
28  */
29 
30 using System;
31 using System.Globalization;
32 
33 namespace Jypeli
34 {
39  public struct Angle
40  {
44  public static readonly Angle Zero = new Angle( 0 );
45 
49  public static readonly Angle RightAngle = new Angle( 0.5 * Math.PI );
50 
54  public static readonly Angle StraightAngle = new Angle( Math.PI );
55 
59  public static readonly Angle FullAngle = new Angle( 2 * Math.PI );
60 
61 
62  private double radian;
63 
68  public double Degrees
69  {
70  get
71  {
72  return RadianToDegree( radian );
73  }
74  set
75  {
76  radian = DegreeToRadian( value );
77  }
78  }
79 
84  public double Radians
85  {
86  get { return radian; }
87  set
88  {
89  radian = AdvanceMath.MathHelper.ClampAngle( (float)value );
90  }
91  }
92 
97  {
98  get
99  {
100  if ( radian >= -Math.PI / 4 && radian <= Math.PI / 4 ) return Direction.Right;
101  if ( radian > Math.PI / 4 && radian < 3 * Math.PI / 4 ) return Direction.Up;
102  if ( radian < -Math.PI / 4 && radian > -3 * Math.PI / 4 ) return Direction.Down;
103  return Direction.Left;
104  }
105  }
106 
110  public double Sin
111  {
112  get { return Math.Sin( this.Radians ); }
113  }
114 
118  public double Cos
119  {
120  get { return Math.Cos( this.Radians ); }
121  }
122 
126  public double Tan
127  {
128  get { return Math.Tan( this.Radians ); }
129  }
130 
131 
132  private Angle( double radians )
133  {
134  this.radian = AdvanceMath.MathHelper.ClampAngle( (float)radians );
135  }
136 
137  #region Operators
138 
145  public static Angle operator +( Angle a, Angle b )
146  {
147  return FromRadians( a.Radians + b.Radians );
148  }
149 
156  public static Angle operator -( Angle a, Angle b )
157  {
159  }
160 
165  public static Angle operator -( Angle a )
166  {
167  return FromRadians( -a.Radians );
168  }
169 
176  public static Angle operator *( double a, Angle b )
177  {
178  return FromRadians( a * b.Radians );
179  }
180 
187  public static Angle operator *( Angle a, double b )
188  {
189  return FromRadians( a.Radians * b );
190  }
191 
198  public static Angle operator /( Angle a, double b )
199  {
200  return FromRadians( a.Radians / b );
201  }
202 
209  public static bool operator ==( Angle a, Angle b )
210  {
211  return a.Radians == b.Radians;
212  }
213 
220  public static bool operator !=( Angle a, Angle b )
221  {
222  return a.Radians != b.Radians;
223  }
224 
231  public static bool operator <( Angle a, Angle b )
232  {
233  return a.Radians < b.Radians;
234  }
235 
242  public static bool operator <=( Angle a, Angle b )
243  {
244  return a.Radians <= b.Radians;
245  }
246 
253  public static bool operator >( Angle a, Angle b )
254  {
255  return a.Radians > b.Radians;
256  }
257 
264  public static bool operator >=( Angle a, Angle b )
265  {
266  return a.Radians >= b.Radians;
267  }
268 
274  public static explicit operator UnlimitedAngle( Angle angle )
275  {
276  return angle.Unlimit();
277  }
278 
285  public static Angle Sum(UnlimitedAngle a, Angle b)
286  {
287  return Angle.FromRadians( a.Radians + b.Radians );
288  }
289 
296  public static Angle Sum( Angle a, UnlimitedAngle b )
297  {
298  return Angle.FromRadians( a.Radians + b.Radians );
299  }
300 
301  #endregion
302 
307  {
308  return UnlimitedAngle.FromRadians( this.radian );
309  }
310 
315  public static Angle FromRadians( double radian )
316  {
317  return new Angle( radian );
318  }
319 
324  public static Angle FromDegrees( double degree )
325  {
326  return new Angle( DegreeToRadian( degree ) );
327  }
328 
334  public static double DegreeToRadian( double degree )
335  {
336  return AdvanceMath.MathHelper.ClampAngle( (float)(degree * ( System.Math.PI / 180 )) );
337  }
338 
344  public static double RadianToDegree( double radian )
345  {
346  double a = AdvanceMath.MathHelper.ClampAngle( (float)radian );
347  return a * ( 180 / System.Math.PI );
348  }
349 
355  public static Angle Complement( Angle a )
356  {
357  return FromRadians( 0.5 * Math.PI - a.Radians );
358  }
359 
365  public static Angle Supplement( Angle a )
366  {
367  return FromRadians( Math.PI - a.Radians );
368  }
369 
375  public static Angle Explement( Angle a )
376  {
377  return FromRadians( 2 * Math.PI - a.Radians );
378  }
379 
386  public override int GetHashCode()
387  {
388  return Convert.ToInt32( Degrees );
389  }
390 
396  public override bool Equals( object obj )
397  {
398  if ( obj is Angle )
399  {
400  return Double.Equals( this.Radians, ( (Angle)obj ).Radians );
401  }
402 
403  return false;
404  }
405 
406  public override string ToString()
407  {
408  return radian.ToString( System.Globalization.NumberFormatInfo.InvariantInfo );
409  }
410 
411  public string ToString( IFormatProvider formatProvider )
412  {
413  return radian.ToString( formatProvider );
414  }
415 
416  public static Angle Parse( string angleStr, IFormatProvider formatProvider )
417  {
418  return new Angle( double.Parse( angleStr, formatProvider ) );
419  }
420 
421  public static Angle Parse( string angleStr )
422  {
423  return new Angle( double.Parse( angleStr, NumberFormatInfo.InvariantInfo ) );
424  }
425 
431  public double GetPositiveRadians()
432  {
433  return Radians >= 0 ? Radians : Math.PI * 2 + Radians;
434  }
435 
441  public double GetPositiveDegrees()
442  {
443  return Degrees >= 0 ? Degrees : 360 + Degrees;
444  }
445 
446  public Vector GetVector()
447  {
448  return Vector.FromAngle( this );
449  }
450 
451  #region Arcusfunktiot
452 
458  public static Angle ArcSin( double d )
459  {
460  return new Angle( Math.Asin( d ) );
461  }
462 
468  public static Angle ArcCos( double d )
469  {
470  return new Angle( Math.Acos( d ) );
471  }
472 
478  public static Angle ArcTan( double d )
479  {
480  return new Angle( Math.Atan( d ) );
481  }
482 
483  #endregion
484  }
485 }
Jypeli.Direction.Left
static Direction Left
Suunta vasemmalle.
Definition: Direction.cs:66
Jypeli.Angle.RadianToDegree
static double RadianToDegree(double radian)
Muuttaa radiaanit asteiksi.
Definition: Angle.cs:344
Jypeli.Angle.GetVector
Vector GetVector()
Definition: Angle.cs:446
Jypeli.Angle.operator<
static bool operator<(Angle a, Angle b)
Vertaa ensimmäisen kulman suuremmuutta toiseen.
Definition: Angle.cs:231
Jypeli.Angle.operator/
static Angle operator/(Angle a, double b)
Jakaa kulman reaaliluvulla.
Definition: Angle.cs:198
Jypeli.Angle.MainDirection
Direction MainDirection
Kulmaa vastaava pääilmansuunta.
Definition: Angle.cs:97
Jypeli.Angle.radian
double radian
Definition: Angle.cs:62
Jypeli.Angle.ArcSin
static Angle ArcSin(double d)
Palauttaa kulman joka vastaa d:n arcus-sini.
Definition: Angle.cs:458
Jypeli.Angle.operator>
static bool operator>(Angle a, Angle b)
Vertaa ensimmäisen kulman pienemmyyttä toiseen.
Definition: Angle.cs:253
Jypeli.Angle.GetPositiveRadians
double GetPositiveRadians()
Palauttaa kulman radiaaneina siten, että se on aina positiivinen. Hyödyllinen esimerkiksi ympyrän kaa...
Definition: Angle.cs:431
Jypeli.Angle.Cos
double Cos
Kulman kosini.
Definition: Angle.cs:119
Jypeli.Direction.Up
static Direction Up
Suunta ylös.
Definition: Direction.cs:56
Jypeli.Angle.GetPositiveDegrees
double GetPositiveDegrees()
Palauttaa kulman asteina siten, että se on aina positiivinen. Hyödyllinen esimerkiksi ympyrän kaarien...
Definition: Angle.cs:441
Jypeli
Definition: Automobile.cs:5
Jypeli.Angle.Angle
Angle(double radians)
Definition: Angle.cs:132
Jypeli.Direction
Perussuunta tasossa.
Definition: Direction.cs:47
Jypeli.Angle.GetHashCode
override int GetHashCode()
Palauttaa kulmaa yksilöivän luvun, tässä tapauksessa kulman asteluvun.
Definition: Angle.cs:386
Jypeli.Angle.Tan
double Tan
Kulman tangentti.
Definition: Angle.cs:127
Jypeli.UnlimitedAngle
Rajoittamaton kulma (asteina ja radiaaneina). Tietoja kulmasta: http://en.wikipedia....
Definition: UnlimitedAngle.cs:40
Jypeli.Angle.FullAngle
static readonly Angle FullAngle
Täysikulma (360 astetta).
Definition: Angle.cs:59
Jypeli.Angle.operator-
static Angle operator-(Angle a, Angle b)
Vähentää jälkimmäisen kulman ensimmäisestä.
Definition: Angle.cs:156
AdvanceMath.MathHelper
Definition: MathHelper.cs:38
Jypeli.Angle.operator!=
static bool operator!=(Angle a, Angle b)
Vertaa kahden kulman erisuuruutta.
Definition: Angle.cs:220
Jypeli.Angle.operator==
static bool operator==(Angle a, Angle b)
Vertaa kahden kulman yhtäsuuruutta.
Definition: Angle.cs:209
Jypeli.Angle.Radians
double Radians
Palauttaa tai asettaa kulman radiaaneina.
Definition: Angle.cs:85
Jypeli.Direction.Down
static Direction Down
Suunta alas.
Definition: Direction.cs:61
Jypeli.Angle.Unlimit
UnlimitedAngle Unlimit()
Palauttaa vastaavan rajoittamattoman kulman.
Definition: Angle.cs:306
Jypeli.Angle.operator<=
static bool operator<=(Angle a, Angle b)
Vertaa ensimmäisen kulman suuremmuutta/yhtäsuuruutta toiseen.
Definition: Angle.cs:242
Jypeli.Angle.ArcTan
static Angle ArcTan(double d)
Palauttaa kulman joka vastaa d:n arcus-tangentti.
Definition: Angle.cs:478
Jypeli.UnlimitedAngle.FromRadians
static UnlimitedAngle FromRadians(double radian)
Luo kulman annettujen radiaanien mukaan.
Definition: UnlimitedAngle.cs:319
Jypeli.UnlimitedAngle.Radians
double Radians
Palauttaa tai asettaa kulman radiaaneina.
Definition: UnlimitedAngle.cs:89
Jypeli.Angle.Supplement
static Angle Supplement(Angle a)
Laskee suplementtikulman (180 asteen kulman toinen puoli)
Definition: Angle.cs:365
Jypeli.Angle.DegreeToRadian
static double DegreeToRadian(double degree)
Muuttaa asteet radiaaneiksi.
Definition: Angle.cs:334
Jypeli.Angle.FromRadians
static Angle FromRadians(double radian)
Luo kulman annettujen radiaanien mukaan.
Definition: Angle.cs:315
Jypeli.Angle.Sin
double Sin
Kulman sini.
Definition: Angle.cs:111
Jypeli.Angle.Zero
static readonly Angle Zero
Nollakulma.
Definition: Angle.cs:44
Jypeli.Vector.FromAngle
static Vector FromAngle(Angle angle)
Luo vektorin kulman perusteella yksikköpituudella.
Definition: Vector.cs:125
Jypeli.Angle.Sum
static Angle Sum(UnlimitedAngle a, Angle b)
Laskee yhteen rajoittamattoman ja rajoitetun kulman, palauttaen rajoitetun kulman.
Definition: Angle.cs:285
Jypeli.Angle.operator*
static Angle operator*(double a, Angle b)
Kertoo kulman reaaliluvulla.
Definition: Angle.cs:176
Jypeli.Angle.operator>=
static bool operator>=(Angle a, Angle b)
Vertaa ensimmäisen kulman pienemmyyttä/yhtäsuuruutta toiseen.
Definition: Angle.cs:264
Jypeli.Angle.ToString
string ToString(IFormatProvider formatProvider)
Definition: Angle.cs:411
Jypeli.Angle.operator+
static Angle operator+(Angle a, Angle b)
Laskee kaksi kulmaa yhteen.
Definition: Angle.cs:145
Jypeli.Angle.Complement
static Angle Complement(Angle a)
Laskee komplementtikulman (90 asteen kulman toinen puoli)
Definition: Angle.cs:355
Jypeli.Angle.Explement
static Angle Explement(Angle a)
Laskee eksplementtikulman (360 asteen kulman toinen puoli)
Definition: Angle.cs:375
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.Angle.RightAngle
static readonly Angle RightAngle
Suora kulma (90 astetta).
Definition: Angle.cs:49
Jypeli.Angle.Sum
static Angle Sum(Angle a, UnlimitedAngle b)
Laskee yhteen rajoitetun ja rajoittamattoman kulman, palauttaen rajoitetun kulman.
Definition: Angle.cs:296
AdvanceMath.MathHelper.ClampAngle
static Scalar ClampAngle(Scalar angle)
Definition: MathHelper.cs:164
Jypeli.Angle.FromDegrees
static Angle FromDegrees(double degree)
Luo kulman annettujen asteiden mukaan.
Definition: Angle.cs:324
Jypeli.Angle.Equals
override bool Equals(object obj)
Tarkistaa kahden kulman yhtäsuuruuden. Jos parametrinä annetaan jotain muuta kuin kulma,...
Definition: Angle.cs:396
Jypeli.Angle.Degrees
double Degrees
Palauttaa tai asettaa kulman asteina.
Definition: Angle.cs:69
Jypeli.Angle.Parse
static Angle Parse(string angleStr, IFormatProvider formatProvider)
Definition: Angle.cs:416
Jypeli.Angle.Parse
static Angle Parse(string angleStr)
Definition: Angle.cs:421
Jypeli.Angle.ArcCos
static Angle ArcCos(double d)
Palauttaa kulman joka vastaa d:n arcuskosini.
Definition: Angle.cs:468
AdvanceMath
Definition: Clamped.cs:36
Jypeli.Direction.Right
static Direction Right
Suunta oikealle.
Definition: Direction.cs:71
Jypeli.Angle.StraightAngle
static readonly Angle StraightAngle
Oikokulma (180 astetta).
Definition: Angle.cs:54
Jypeli.Angle.ToString
override string ToString()
Definition: Angle.cs:406
Jypeli.Angle
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40