Пример #1
0
        public override object GetCopy()
        {
            Mirror obj = new Mirror();

            obj.Angle        = this.angle;
            obj.defaultColor = this.defaultColor;
            obj.focusColor   = this.focusColor;
            obj.height       = this.height;
            obj.width        = this.width;
            obj.x            = this.x;
            obj.y            = this.y;
            return(obj);
        }
        //Input: Light list, graphics g and a Physical object
        //Output: Change the Light list to the path casued by the Mirror
        public static void MirrorAlgoritem(List <Light> lightList, Graphics g, PhysicalObject obj)
        {
            Light  last       = lightList[lightList.Count - 1];
            PointF pi         = last.pi;
            PointF pf         = last.pf;
            PointF firstPoint = new PointF(pi.X, pi.Y);
            Color  lightColor = last.LightColor;
            Region mirrorReg  = obj.GetRegion();
            Region rayReg     = last.GetRegion();
            double angle      = last.GetAngle();
            bool   vertical   = false;


            mirrorReg.Intersect(rayReg); //Intersect area of lens and the last light
            if (!mirrorReg.IsEmpty(g))
            {
                PointF hit = GetHitPoint(pf, mirrorReg, g);

                lightList[lightList.Count - 1] = new Light(pi, hit, lightColor); //light from the start to the hit point

                double alpha = 180 - (angle - 2 * obj.Angle);

                //check if the light hiting the vertical part of the mirror
                PointF[] points = obj.GetPoints();
                if (points.Length == 4)
                {
                    PointF p1     = points[0];
                    PointF p2     = points[1];
                    PointF p3     = points[2];
                    PointF p4     = points[3];
                    float  x      = hit.X;
                    float  y      = hit.Y;
                    bool   check1 = x >= Math.Min(p1.X, p4.X) && x <= Math.Max(p1.X, p4.X) && y >= Math.Min(p1.Y, p4.Y) && y <= Math.Max(p1.Y, p4.Y);
                    bool   check2 = x >= Math.Min(p2.X, p3.X) && x <= Math.Max(p2.X, p3.X) && y >= Math.Min(p2.Y, p3.Y) && y <= Math.Max(p2.Y, p3.Y);
                    vertical = check1 || check2;
                    if (vertical)
                    {
                        alpha += 180;
                    }
                }
                lightList.Add(new Light(hit, MathHelper.GetEndLight(hit, alpha), lightColor)); //light from the hit point farther

                //draw image and give info
                Color  imageColor = Color.Blue;
                Mirror mirror     = obj as Mirror;
                if (mirror != null && mirror.ShowImage)
                {
                    //calc image
                    imageColor = mirror.ImageColor;
                    double beta      = alpha + 180;
                    double radius    = MathHelper.DistanceBetweenPointF(hit, firstPoint);
                    float  height    = (float)(radius * Math.Sin(beta * Math.PI / 180));
                    float  width     = (float)(radius * Math.Cos(beta * Math.PI / 180));
                    float  addWidth  = obj.X - hit.X;
                    float  addHeight = obj.Y - hit.Y;
                    //

                    //draw image
                    PointF imagePoint    = new PointF(obj.X + width + addWidth, obj.Y - height + addHeight);
                    double angleToRotate = obj.Angle;
                    if (angleToRotate >= 180 && angleToRotate <= 360)
                    {
                        angleToRotate = angleToRotate + 180;
                    }
                    imagePoint = MathHelper.RotatePointF(imagePoint, obj.CenterPoint, angleToRotate);
                    g.FillEllipse(new SolidBrush(imageColor), imagePoint.X - 5, imagePoint.Y - 5, 10, 10);
                    //

                    //give information to data class
                    if (Main_Form.highlightedIndex != -1)
                    {
                        PhysicalObject highlighted = Main_Form.objectsList[Main_Form.highlightedIndex];
                        if (highlighted == obj)
                        {
                            MirrorInfo.startPoint = firstPoint;
                            //MirrorInfo.imagePoint = imagePoint;
                        }
                    }
                }
                //
            }
        }