Beispiel #1
0
 public void Run()
 {
     Vec v = new Vec(1.0,1.0);
     Console.WriteLine("v={0}", v);
     v = v.Scale(5.0).Add(new Vec(3.0,-2.0));
     Console.WriteLine("v={0}", v);
 }
Beispiel #2
0
        public void TestSort()
        {
            var list = new List<Vec>();
            const int len = 10;
            list.Capacity = len;
            for (int i = 0; i < len; ++i) {
                var s = new Vec(GetF(100.0), GetF(50.0));
                list.Add(s);
            }

            Console.WriteLine("The list is [{0}]", string.Join(", ", list));
            list.Sort(this.compareVecByX);
            Console.WriteLine("The list is [{0}]", string.Join(", ", list));

            Assert.That(false);
        }
Beispiel #3
0
 public RibRect(Rib r)
 {
     var radius = r.Sprite.Radius;
     min = r.StartPos;
     max = r.EndPos;
     if (max.X < min.X) {
         var t = max.X;
         max.X = min.X;
         min.X = t;
     }
     if (max.Y < min.Y) {
         var t = max.Y;
         max.Y = min.Y;
         min.Y = t;
     }
     min.X -= radius;
     min.Y -= radius;
     max.X += radius;
     max.Y += radius;
 }
Beispiel #4
0
 public Vec Add(Vec x)
 {
     var tmp = this;
     tmp.X += x.X;
     tmp.Y += x.Y;
     return tmp;
 }
Beispiel #5
0
 public Vec Rotate(Vec a)
 {
     return Rotate(a.X, a.Y);
 }
Beispiel #6
0
 void OnMouseUp(object sender, MouseEventArgs a)
 {
     if (a.Button == MouseButtons.Left && !mouseDownLocation.IsEmpty) {
         U.show("OnMouseUp");
         var s = new Sprite(images[curimage], 0.0F);
         s.Position = new Vec(mouseDownLocation);
         Vec spd = new Vec(a.Location).Subtract(s.Position).Scale(0.4);
         var speedlen = spd.Length;
         if (speedlen > U.maxspeed) {
             spd = spd.Scale(U.maxspeed/speedlen);
         } else if (speedlen < U.minspeed) {
             // make random speed
             double alpha = Math.PI * 2.0 * random.NextDouble();
             speedlen = (random.NextDouble() * (U.maxspeed-U.minspeed) + U.minspeed);
             spd = Vec.Dir(alpha).Scale(speedlen);
         }
         s.Speed = spd;
         s.Rotation = (float)(random.NextDouble() - 0.5) * 10.0F;
         sprites.Add(s);
         mouseDownLocation = new Point();
         mouseLastLocation = mouseDownLocation;
     } else if (a.Button == MouseButtons.Right) {
         curimage += 1;
         if (curimage >= images.Count) {
             curimage = 0;
         }
         prototype = new Sprite(images[curimage], 0.0F);
     }
 }
Beispiel #7
0
 public Vec Subtract(Vec x)
 {
     var tmp = this;
     tmp.X -= x.X;
     tmp.Y -= x.Y;
     return tmp;
 }
Beispiel #8
0
 public Rot(double alpha)
 {
     rot = new Vec(Math.Cos(alpha), Math.Sin(alpha));
 }
Beispiel #9
0
 // return a tangental projection
 public Vec Project(Vec v)
 {
     return v.Scale(this.Scalar(v)/v.Length2);
 }
Beispiel #10
0
 public double Scalar(Vec v)
 {
     return X * v.X + Y * v.Y;
 }
Beispiel #11
0
 private int compareVecByX(Vec a, Vec b)
 {
     return (a.X < b.X) ? -1 : ((a.X > b.X) ? 1 : 0);
 }
Beispiel #12
0
 public Vec Normal(Vec v)
 {
     return this.Subtract(Project(v));
 }
Beispiel #13
0
 public Sprite(Bitmap theimg, double themass)
     : this()
 {
     image = theimg;
     Speed = new Vec();
     alpha = omega = 0.0;
     // init is the initial radius
     var init = Math.Min(image.Size.Width, image.Size.Height) * 0.5;
     if (themass <= 0.001) {
         themass = init * init;
     }
     mass = themass;
     radius = Math.Sqrt(mass);
     scale = radius / init * 0.5;
     Position = new Vec(image.Size).Scale(scale);
 }
Beispiel #14
0
 public void Update(Rib rib)
 {
     Position = rib.EndPos;
     Speed    = rib.Speed;
     alpha   += omega * rib.DeltaTime;
 }
Beispiel #15
0
 public void Update(Rib rib)
 {
     Position = rib.EndPos;
     Speed = rib.Speed;
     alpha += omega * rib.DeltaTime;
 }
Beispiel #16
0
 public void Draw(Graphics g)
 {
     PointF[] dest = new PointF[3];
     Rot rotation = new Rot(alpha);
     Vec sz = new Vec(image.Size).Scale(scale);
     // x + px*cosa+py*sina, y - px*sina + py*cosa
     dest[0] = Position.Add(rotation.Rotate(-sz.X, -sz.Y)).Point;
     dest[1] = Position.Add(rotation.Rotate(sz.X, -sz.Y)).Point;
     dest[2] = Position.Add(rotation.Rotate(-sz.X, sz.Y)).Point;
     g.DrawImage(image, dest);
 }
Beispiel #17
0
 public Sprite(Bitmap theimg, Sprite one, Sprite two)
     : this(theimg, one.mass + two.mass)
 {
     // now calculate the position, speed and rotation
     double invertmass = 1.0/mass;
     Position = one.Position.Scale(one.Mass).Add(two.Position.Scale(two.Mass)).Scale(invertmass);
     Speed = one.Speed.Scale(one.Mass).Add(two.Speed.Scale(two.mass)).Scale(invertmass);
     alpha = (Math.IEEERemainder(one.alpha,Math.PI*2) * one.AngularMass + Math.IEEERemainder(two.alpha,Math.PI*2) * two.AngularMass) / AngularMass;
     // sum up all moments of rotation
     double totalRotationMomentum =
         one.Mass * one.Position.Subtract(Position).Normal(one.Speed).Length +
         two.Mass * two.Position.Subtract(Position).Normal(two.Speed).Length +
         one.AngularMass * one.omega + two.AngularMass * two.omega;
     omega = totalRotationMomentum / AngularMass;
 }