예제 #1
0
        public Polygon CutPolygon(Polygon P)
        {
            PointF        I, Tmp1, Tmp2;
            Vector        Nv, D, W;
            List <PointF> Q = new List <PointF>();

            Peaks.Add(Peaks[0]);

            double Dsk, Wsk, t;

            // Обход по всем сторонам отсекателя
            for (int i = 1; i < Peaks.Count; i++)
            {
                Nv = GetNormal(Peaks[i - 1], Peaks[i]);

                if (VisiblePeak(P.GetPeak(0), Peaks[i], Nv))
                {
                    Q.Add(P.GetPeak(0));
                }

                // Обход по всем сторонам многоугольника
                for (int j = 1; j < P.Count(); j++)
                {
                    Tmp1 = P.GetPeak(j - 1);
                    Tmp2 = P.GetPeak(j);

                    // Вектор отрезка
                    D = new Vector(Tmp2.X - Tmp1.X, Tmp2.Y - Tmp1.Y);
                    // Угол и с какой он стороны
                    Dsk = ScalarMultVector(D, Nv);

                    // Проверка, что отрезок не вырождается в точку и не параллелен
                    if (Dsk != 0)
                    {
                        // Вектор, который соединяет начало отрезка и вершину многоугольника
                        W   = new Vector(Tmp1.X - Peaks[i - 1].X, Tmp1.Y - Peaks[i - 1].Y);
                        Wsk = ScalarMultVector(W, Nv);

                        t = -Wsk / Dsk;

                        if (t >= 0 && t <= 1)
                        {
                            I = new PointF((float)(P.GetPeak(j - 1).X + t * D.X), (float)(P.GetPeak(j - 1).Y + t * D.Y));
                            Q.Add(I);
                        }
                    }

                    if (VisiblePeak(P.GetPeak(j), Peaks[i], Nv))
                    {
                        Q.Add(P.GetPeak(j));
                    }
                }

                Q.Add(Q[0]);
                P.Clear();
                P.AddRange(Q);
                Q.Clear();
            }

            return(P);
        }
예제 #2
0
        public void AnalyzeSpectrum()
        {
            try
            {
                Peaks.Clear();
                PlotPoints.Clear();
                if (firstAnalyze)
                {
                    SamplePeaks.Clear();
                }

                var    uri    = new Uri(PicPath);
                var    bitmap = new BitmapImage(uri);
                int    stride = bitmap.PixelWidth * 4;
                int    size   = bitmap.PixelHeight * stride;
                byte[] pixels = new byte[size];
                bitmap.CopyPixels(pixels, stride, 0);
                //make an average values
                //1. get sum of each color components
                int[] avgpixels = new int[stride];
                for (int y = 0; y < bitmap.PixelHeight; y++)
                {
                    for (int x = 0; x < bitmap.PixelWidth; x++)
                    {
                        int index = y * stride + 4 * x;
                        avgpixels[4 * x]     += pixels[index];
                        avgpixels[4 * x + 1] += pixels[index + 1];
                        avgpixels[4 * x + 2] += pixels[index + 2];
                    }
                }

                //2. get an average values - smooth
                for (int x = 0; x < bitmap.PixelWidth; x++)
                {
                    int index = bitmap.PixelHeight * stride + 4 * x;
                    avgpixels[4 * x]     /= bitmap.PixelHeight;
                    avgpixels[4 * x + 1] /= bitmap.PixelHeight;
                    avgpixels[4 * x + 2] /= bitmap.PixelHeight;
                }

                //get intensity and make points on plot
                for (int x = 0; x < bitmap.PixelWidth; x++)
                {
                    var spec  = new AnalyzedSpectrum();
                    int index = 4 * x;
                    int red   = avgpixels[index];
                    int green = avgpixels[index + 1];
                    int blue  = avgpixels[index + 2];
                    spec.PeakPixel     = x + 380;                                                                                   //scale to wavelenght
                    spec.PeakIntensity = Math.Round(Math.Sqrt(0.299 * red * red + 0.587 * green * green + 0.114 * blue * blue), 2); // hsp color model
                    Peaks.Add(spec);
                    if (firstAnalyze)
                    {
                        SamplePeaks.Add(spec);
                    }

                    var _point = new DataPoint(spec.PeakPixel, spec.PeakIntensity);
                    PlotPoints.Add(_point);
                }

                firstAnalyze = false;
            }
            catch (Exception e)
            {
                MessageBox.Show("You must make a photo of spectrum first!");
            }
        }