聚类算法图形展示程序(WPF)

来源:互联网 发布:js div隐藏 编辑:程序博客网 时间:2024/05/23 00:02

实现原理:Canvas中添加Ellipse作为数据点,根据cluster进行染色。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.IO;using System.Drawing;using System.Windows.Forms;namespace Clustering_show{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            System.Windows.Media.Color[] color = new System.Windows.Media.Color[15];            color[0] = System.Windows.Media.Colors.Red;            color[1] = System.Windows.Media.Colors.Green;            color[2] = System.Windows.Media.Colors.Blue;            color[3] = System.Windows.Media.Colors.Orange;            color[4] = System.Windows.Media.Colors.Purple;            color[5] = System.Windows.Media.Colors.Black;            color[6] = System.Windows.Media.Colors.Pink;            color[7] = System.Windows.Media.Colors.Cyan;            color[8] = System.Windows.Media.Colors.Gray;            color[9] = System.Windows.Media.Colors.Tomato;            color[10] = System.Windows.Media.Colors.AliceBlue;            color[11] = System.Windows.Media.Colors.AntiqueWhite;            color[12] = System.Windows.Media.Colors.Aqua;            color[13] = System.Windows.Media.Colors.Aquamarine;            color[14] = System.Windows.Media.Colors.Azure;            int ratio = 1;            int hw = 7;            if (System.Windows.Forms.MessageBox.Show("是否放大", "Confirm Message", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)            {                ratio = 15;            }            Microsoft.Win32.OpenFileDialog openFile = new Microsoft.Win32.OpenFileDialog();            openFile.Filter = "文本文件(*.txt)|*.txt|(*.rtf)|*.rtf";            if (openFile.ShowDialog() == true)            {                using (StreamReader sr = new StreamReader(openFile.FileName, Encoding.Default))                {                    while (sr.Peek() > 0)                    {                        string line;                        while ((line = sr.ReadLine()) != null)                        {                            String[] ss = line.Split(',');                            Ellipse e = new Ellipse();                            e.Height = hw;                            e.Width = hw;                            int c = Convert.ToInt32(ss[2]);                            e.Fill = new SolidColorBrush(color[c - 1]);//k-means                            //e.Fill = new SolidColorBrush//(System.Windows.Media.Color.FromRgb(Convert.ToByte(c*10 % 250), Convert.ToByte(c*15 % 250), Convert.ToByte(c % 250)));//DBCAN&&Hierarchical                            Double x = Convert.ToDouble(ss[0]);                            Double y = Convert.ToDouble(ss[1]);                            e.Stroke = System.Windows.Media.Brushes.White;                            e.SetValue(Canvas.LeftProperty, x * ratio);                            e.SetValue(Canvas.TopProperty, y * ratio);                            pointCanvas.Children.Add(e);                        }                    }                }            }        }    }}
本人博客中DBSCAN,Hierarchical(MIN),K-means算法均由此程序进行图形展现。

0 0