示例#1
0
        public override object GetCopy()
        {
            Lens obj = new Lens(this.Type);

            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;
            obj.radius1      = this.radius1;
            obj.radius2      = this.radius2;
            return(obj);
        }
        //Input: Light list, graphics g and a Lens
        //Output: Change the Light list to the path casued by the lens
        public static void LensAlgoritem(List <Light> lightList, Graphics g, Lens obj)
        {
            try
            {
                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;
                double initialAngle = last.GetAngle();

                Region lensReg = obj.GetRegion();
                Region rayReg  = last.GetRegion();


                lensReg.Intersect(rayReg); //Intersect area of lens and the last light
                if (!lensReg.IsEmpty(g))
                {
                    //getting the hit point
                    RectangleF boundsRect = lensReg.GetBounds(g);
                    PointF     hit        = new PointF((boundsRect.Right + boundsRect.Left) / 2, (boundsRect.Top + boundsRect.Bottom) / 2);
                    pf = hit;

                    //creating a light bewtween initial point and hit point
                    lightList[lightList.Count - 1] = new Light(pi, pf, lightColor);

                    pi = pf;

                    Light  light = new Light(pi, pf);
                    double beta  = 0;

                    beta = GetImagePointAngle(g, firstPoint, obj, initialAngle, hit);

                    //creating a light between the hit point and on
                    light = new Light(hit, MathHelper.GetEndLight(hit, beta), lightColor);
                    lightList.Add(light);
                }
            }
            catch
            {
            }
        }
        //Input: Graphics g and a lens
        //Output: Draw the either the focal points of the lens or/and the lens optical axis
        public static void DrawFocalPointsAndOpticalAxis(Graphics g, Lens lens)
        {
            //draw the focal points of the lens who got hit
            PointF focal1 = new PointF(lens.X + (float)lens.FocalPoint - 5, lens.Y - 5);
            PointF focal2 = new PointF(lens.X - (float)lens.FocalPoint - 5, lens.Y - 5);

            focal1 = MathHelper.RotatePointF(lens, focal1);
            focal2 = MathHelper.RotatePointF(lens, focal2);
            if (lens.ShowFocalPoints)
            {
                g.FillEllipse(Brushes.BlueViolet, focal1.X, focal1.Y, 10, 10);
                g.FillEllipse(Brushes.BlueViolet, focal2.X, focal2.Y, 10, 10);
            }
            if (lens.ShowOpticalAxis)
            {
                Light l1 = new Light(lens.CenterPoint, MathHelper.GetEndLight(lens.CenterPoint, lens.Angle));
                Light l2 = new Light(lens.CenterPoint, MathHelper.GetEndLight(lens.CenterPoint, lens.Angle + 180));
                g.DrawLine(Pens.DarkBlue, l1.pi, l1.pf);
                g.DrawLine(Pens.DarkBlue, l2.pi, l2.pf);
            }
        }
        //Input: Graphics g, point which the last light start from, Lens object, angle of last light and the hit point
        //Output: calculate the angle in which the light comes out of the lens
        public static double GetImagePointAngle(Graphics g, PointF firstPoint, Lens obj, double angle, PointF hit)
        {
            bool imaginary = false;

            float f  = (float)obj.FocalPoint;
            float u  = (float)Math.Abs(obj.X - firstPoint.X);
            float ho = firstPoint.Y - obj.Y;
            float v  = 1 / ((1 / f) - (1 / u));
            float hi = (v * ho / u);

            PointF imagePoint = new PointF();

            bool isVertical = obj.Angle > 225 && obj.Angle < 315 || obj.Angle > 45 && obj.Angle < 135;

            if (!isVertical)
            {
                Color imageColor = obj.RealImageColor;
                if (v * u < 0)
                {
                    imageColor = obj.ImaginaryImageColor;
                    imaginary  = true;
                }
                if (firstPoint.X > obj.X)
                {
                    v *= -1;
                }

                imagePoint = new PointF(obj.X + (float)(v), obj.Y - (float)hi);

                /*double angleToRotate = obj.Angle;
                 * if (angleToRotate >= 90 && angleToRotate <= 270) angleToRotate = angleToRotate + 180;
                 * imagePoint = MathHelper.RotatePointF(imagePoint, obj.CenterPoint, 0*angleToRotate);*/

                //drawing image
                if (obj.ShowImage)
                {
                    g.FillEllipse(new SolidBrush(imageColor), imagePoint.X - 5, imagePoint.Y - 5, 10, 10);
                }
            }
            else
            {
                u  = (float)Math.Abs(obj.Y - firstPoint.Y);
                ho = -firstPoint.X + obj.X;
                v  = 1 / ((1 / f) - (1 / u));
                hi = (v * ho / u);

                Color imageColor = obj.RealImageColor;
                if (v * u < 0)
                {
                    imageColor = obj.ImaginaryImageColor;
                    imaginary  = true;
                }
                if (firstPoint.Y > obj.Y)
                {
                    v *= -1;
                }

                imagePoint = new PointF(obj.X + (float)(v), obj.Y - (float)hi);

                /*double angleToRotate = obj.Angle;
                 * if (angleToRotate >= 0 && angleToRotate <= 180) angleToRotate = angleToRotate + 180;
                 * imagePoint = MathHelper.RotatePointF(imagePoint, obj.CenterPoint, angleToRotate);*/

                //drawing image
                if (obj.ShowImage)
                {
                    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)
                {
                    LensInfo.focal = Math.Round(f, 3);
                    LensInfo.u     = Math.Round(u, 3);
                    LensInfo.v     = Math.Round(-v, 3);
                    LensInfo.ho    = Math.Round(-ho, 3);
                    LensInfo.hi    = Math.Round(hi, 3);
                }
            }

            double angleToReturn = new Light(hit, imagePoint).GetAngle();

            if (imaginary)
            {
                angleToReturn = new Light(hit, imagePoint).GetAngle() + 180;
            }

            return(angleToReturn);
        }