// Create a version of the with the passed-in offset. public void SetOrigin(MyPointF origin) { for (int index = 0; index <= sides; index++) { shape[index].X = proto.shape[index].X + (int)origin.X; shape[index].Y = proto.shape[index].Y + (int)origin.Y; } }
public TitleBlock(string title, string fontName, float size, Color color, Size formSize, Graphics graphics) { this.title = title; this.fontName = fontName; this.size = size; this.color = color; this.formSize = formSize; font = new Font(fontName, size); brush = new SolidBrush(color); pen = new Pen(brush); SizeF stringSize = graphics.MeasureString(title, font); PointF location = new PointF(formSize.Width / 2 - stringSize.Width / 2, formSize.Height / 2 - stringSize.Height / 2); titleRectangle = new Rectangle( (int)(formSize.Width / 2 - stringSize.Width / 2), (int)(formSize.Height / 2 - stringSize.Height / 2), (int)stringSize.Width, (int)stringSize.Height); titleDrawSpot = new MyPointF(titleRectangle.X, titleRectangle.Y); float x1 = titleRectangle.X - textPad; float y1 = titleRectangle.Y - textPad; float x2 = titleRectangle.X + titleRectangle.Width + textPad; float y2 = titleRectangle.Y + titleRectangle.Height + textPad; #if fred segments.Add(new Segment(x1, y1, x1, y2)); segments.Add(new Segment(x1, y2, x2, y2)); segments.Add(new Segment(x2, y2, x2, y1)); segments.Add(new Segment(x2, y1, x1, y1)); #endif MyPointF p1 = new MyPointF(x1 + textPad, y1); MyPointF p2 = new MyPointF(x2 - textPad, y1); MyPointF p3 = new MyPointF(x2, y1 + textPad); MyPointF p4 = new MyPointF(x2, y2 - textPad); MyPointF p5 = new MyPointF(x2 - textPad, y2); MyPointF p6 = new MyPointF(x1 + textPad, y2); MyPointF p7 = new MyPointF(x1, y2 - textPad); MyPointF p8 = new MyPointF(x1, y1 + textPad); segments.Add(new Segment(p1, p2)); segments.Add(new Segment(p2, p3)); segments.Add(new Segment(p3, p4)); segments.Add(new Segment(p4, p5)); segments.Add(new Segment(p5, p6)); segments.Add(new Segment(p6, p7)); segments.Add(new Segment(p7, p8)); segments.Add(new Segment(p8, p1)); }
// Description: Place the ball randomly within the window (but not too close to the edge) public void RandLoc() { int height = TheForm.ClientSize.Height; int width = TheForm.ClientSize.Width; position = new MyPointF( (float)(size * 2 + (width - size * 4) * rand.NextDouble()), (float)(size * 2 + (height - size * 4) * rand.NextDouble())); topLeft = new MyPointF(size, size); bottomRight = new MyPointF(width - size, height - size); }
// is the point inside of the rectangle? public bool IsInside(MyPointF point, float maxSize) { if ((point.X > titleRectangle.X - maxSize) && (point.X < titleRectangle.X + titleRectangle.Width + maxSize) && (point.Y > titleRectangle.Y - maxSize) && (point.Y < titleRectangle.Y + titleRectangle.Height + maxSize)) { return(true); } else { return(false); } }
Ball collideBall = new Ball(); // ball used for collisions public Segment(MyPointF p1, MyPointF p2) { this.p1 = p1; this.p2 = p2; vUnit = new Vector(p2.X - p1.X, p2.Y - p1.Y).MakeUnit();; }
public static float Distance(MyPointF pt1, MyPointF pt2) { MyPointF delta = pt1 - pt2; return((float)Math.Sqrt(delta.X * delta.X + delta.Y * delta.Y)); }
public Vector(MyPointF p1, MyPointF p2) { this.x = p2.X - p1.X; this.y = p2.Y - p1.Y; }
public Vector(MyPointF point) { this.x = point.X; this.y = point.Y; }
// Description: Handle gravity and collisions between the current ball and another public void GravityCollide(Ball ball2) { /* * If we're doing gravity, the calculations can be reused for collisions (if enabled) * If we're doing collisions only, we have some short-circuit cases... */ if (setup.gravityBall != 0) { MyPointF dist = position - ball2.position; float distSq = dist.X * dist.X + dist.Y * dist.Y; /* * Collision (no short-circuit - already have distance squared) */ if (setup.collisions) { int centerDistance = size + ball2.size; int centerDistanceSq = centerDistance * centerDistance; if (distSq <= centerDistanceSq) { Collide(ball2, true); distSq = 0; // set to zero to disable gravity for this iteration } } if (distSq != 0) { float distCu = (float)(Math.Sqrt(distSq) * distSq); float sx = (setup.gravityBall * gval * dist.X); float sy = (setup.gravityBall * gval * dist.Y); velocity.X -= (sx * ball2.mass) / distCu; velocity.Y -= (sy * ball2.mass) / distCu; ball2.velocity.X += (sx * mass) / distCu; ball2.velocity.Y += (sy * mass) / distCu; } } else { /* * No gravity, optimize collision */ if (!setup.collisions) { return; } /* * Trivial rejection... */ float centerDistance = size + ball2.size; float distx = Math.Abs(position.X - ball2.position.X); if (distx > centerDistance) { return; } float disty = Math.Abs(position.Y - ball2.position.Y); if (disty > centerDistance) { return; } /* * Passed trivial reject. Are we really close enough? * We factor out sqrt() operation to speed things up, and to gain precision */ float centerDistanceSq = centerDistance * centerDistance; float distSq = distx * distx + disty * disty; if (distSq > centerDistanceSq) { return; } /* * Do the collision! */ Collide(ball2, true); } }