public void hitungCentroid() { double total_latitude_cluster_i = 0; double total_longitude_cluster_i = 0; double total_brightness_cluster_i = 0; double total_confidence_cluster_i = 0; double banyak_anggota_cluster_i = 0; if (iterasi == 0) { sigma_centroid_lama = 0; } else { sigma_centroid_lama = sigma_centroid_baru; } sigma_centroid_baru = 0; for (int i = 0; i < k; i++) { foreach (datakebakaran x in list) { if (x.cluster == i + 1) { total_latitude_cluster_i += x.latitude; total_longitude_cluster_i += x.longitude; total_brightness_cluster_i += x.brightness; total_confidence_cluster_i += x.confidence; banyak_anggota_cluster_i += 1; } } c[i] = new centroid(total_latitude_cluster_i / banyak_anggota_cluster_i, total_longitude_cluster_i / banyak_anggota_cluster_i, total_brightness_cluster_i / banyak_anggota_cluster_i, total_confidence_cluster_i / banyak_anggota_cluster_i); sigma_centroid_baru = sigma_centroid_baru + (total_latitude_cluster_i / banyak_anggota_cluster_i) + (total_longitude_cluster_i / banyak_anggota_cluster_i) + (total_brightness_cluster_i / banyak_anggota_cluster_i) + (total_confidence_cluster_i / banyak_anggota_cluster_i); } }
private static void AddKeyPress(int vkCode) { Debug.WriteLine("AddKeyPress at " + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString() + ":" + DateTime.Now.Millisecond.ToString()); KeysConverter kc = new KeysConverter(); string keyChar = kc.ConvertToString(vkCode).ToUpper(); string keyOut = ""; switch (keyChar) { case "1": keyOut = "ONE"; break; case "2": keyOut = "TWO"; break; case "3": keyOut = "THREE"; break; case "4": keyOut = "FOUR"; break; case "5": keyOut = "FIVE"; break; case "6": keyOut = "SIX"; break; case "7": keyOut = "SEVEN"; break; case "8": keyOut = "EIGHT"; break; case "9": keyOut = "NINE"; break; case "0": keyOut = "ZERO"; break; default: keyOut = keyChar; break; } try { //KeyCodes.ScanCode code = (KeyCodes.ScanCode)Enum.Parse(typeof(KeyCodes.ScanCode), keyOut); var query = from KeyCodes.keyPosition k in KeyCodes.keypositions where k.keyname == keyOut //(int)code select k; centroid c = new centroid(); c.point = new Point((int)query.First().x, (int)query.First().y); c.countup = 1; System.Drawing.Color c1 = Form1.m_startColour; System.Drawing.Color c2 = Form1.m_endColour; if (Form1.randomColours) { Random rndcol = new Random(); c1 = System.Drawing.Color.FromArgb(rndcol.Next(30, 255), rndcol.Next(30, 255), rndcol.Next(30, 255)); c2 = System.Drawing.Color.FromArgb(rndcol.Next(30, 255), rndcol.Next(30, 255), rndcol.Next(30, 255)); } ColorManagment.ColorConverter Converter = new ColorManagment.ColorConverter(); //create a new instance of a ColorConverter ColorRGB rgb1 = new ColorRGB(RGBSpaceName.sRGB, c1.R, c1.G, c1.B); //create an RGB color c.lab1 = Converter.ToLab(rgb1); ColorRGB rgb2 = new ColorRGB(RGBSpaceName.sRGB, c2.R, c2.G, c2.B); //create an RGB color c.lab2 = Converter.ToLab(rgb2); if (centroids.Count > 0) { for (int i = centroids.Count-1; i > -1; i--) { Debug.WriteLine(i); if (centroids[i].point == c.point) { centroids.RemoveAt(i); break; } } } centroids.Add(c); //DoAnimation(); } catch (Exception ex) { Debug.WriteLine("AddKeyPress: handled exception: " + ex.Message); } }
private static void DoAnimation() { //return; //WaveTimer.Enabled = false; if (centroids.Count == 0 || Form1.spellText != "") { //LogitechGSDK.LogiLedSetLighting(0, 0, 0); //WaveTimer.Enabled = true; if (Form1.spellText == "" && Form1.useLogitechColours && ((bw_Keysave != null && !bw_Keysave.IsBusy) || (bw_Breathe != null && !bw_Breathe.IsBusy) || (bw_Breathe == null && bw_Keysave == null)) && isAnimated) { //LogitechGSDK.LogiLedSetLighting(0, 0, 0); LogitechGSDK.LogiLedRestoreLighting(); } isAnimated = false; return; } else { isAnimated = true; //calculate the 2 end point colours into LAB space //System.Drawing.Color c1 = Form1.m_startColour; //System.Drawing.Color c2 = Form1.m_endColour; int fadespeed = Form1.m_fadespeed; int gradientspeed = Form1.m_gradientspeed; //ColorManagment.ColorConverter Converter = new ColorManagment.ColorConverter(); //create a new instance of a ColorConverter //ColorRGB rgb1 = new ColorRGB(RGBSpaceName.sRGB, c1.R , c1.G, c1.B ); //create an RGB color //ColorLab lab1 = Converter.ToLab(rgb1); //ColorRGB rgb2 = new ColorRGB(RGBSpaceName.sRGB, c2.R , c2.G, c2.B ); //create an RGB color //ColorLab lab2 = Converter.ToLab(rgb2); //bmp = new Bitmap(21, 6); ColorLab[,] lab1 = new ColorLab[21, 6]; ColorLab[,] lab2 = new ColorLab[21, 6]; for (int x = 0; x < 21; x++) { for (int y = 0; y < 6; y++) { distances[x, y] = double.MaxValue; times[x, y] = int.MaxValue; } } for (int i = 0; i < centroids.Count; i++) { centroid c = centroids[i]; for (int x = 0; x < 21; x++) { for (int y = 0; y < 6; y++) { double distance = Math.Sqrt(((x - c.point.X) * (x - c.point.X) + (y - c.point.Y) * (y - c.point.Y))); distance = Math.Abs(distance) / (Form1.m_distanceFalloff / 2); if (Form1.m_Wave) { distance -= c.countup; distance = Math.Abs(distance); } if ((distance + c.countup) < (Math.Abs(distances[x, y] + times[x, y]))) { distances[x, y] = distance; lab1[x, y] = c.lab1; lab2[x, y] = c.lab2; if (c.countup / 20.0 < times[x, y]) { times[x, y] = c.countup / 20.0; } } } } c.countup++; centroids[i] = c; } bool allBlack = true; try { //bmp = new Bitmap(21, 6); LockBitmap lockBitmap = new LockBitmap(bmp); lockBitmap.LockBits(); for (int x = 0; x < 21; x++) { for (int y = 0; y < 6; y++) { double distance = distances[x, y]; System.Drawing.Color colour = System.Drawing.Color.White; if (Form1.m_Wave == true) { colour = getColour(lab1[x, y], lab2[x, y], distance + times[x, y] + Math.Pow(distance, Form1.m_WaveSpeed), gradientspeed, fadespeed); } else { colour = getColour(lab1[x, y], lab2[x, y], distance + times[x, y], gradientspeed, fadespeed * 10); } lockBitmap.SetPixel(x, y, colour); if (allBlack && (colour.R > 0 || colour.G > 0 || colour.B > 0)) { allBlack = false; } } } lockBitmap.UnlockBits(); } catch { } finally { } byte[] b = Form1.getLEDGridFromBitmap(bmp); //((Form1)Application.OpenForms[0]).pic1.Image = bmp; //bmp.Save(@"C:\temp\heatmap.png"); Debug.WriteLine("set lighting at " + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString() + ":" + DateTime.Now.Millisecond.ToString()); LogitechGSDK.LogiLedSetLightingFromBitmap(b); if (allBlack) { centroids.Clear(); } else if (!Form1.m_Wave) { for (int i = centroids.Count - 1; i >= 0; i--) { try { if (centroids[i].countup / 10 > 255 / fadespeed) { centroids.RemoveAt(i); } } catch { } } } // System.Threading.Thread.Sleep(Form1.m_AnimationSpeed); } //WaveTimer.Enabled = true; }
private static void AddKeyPress(int vkCode) { Debug.WriteLine("AddKeyPress at " + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString() + ":" + DateTime.Now.Millisecond.ToString()); KeysConverter kc = new KeysConverter(); string keyChar = kc.ConvertToString(vkCode).ToUpper(); string keyOut = ""; switch (keyChar) { case "1": keyOut = "ONE"; break; case "2": keyOut = "TWO"; break; case "3": keyOut = "THREE"; break; case "4": keyOut = "FOUR"; break; case "5": keyOut = "FIVE"; break; case "6": keyOut = "SIX"; break; case "7": keyOut = "SEVEN"; break; case "8": keyOut = "EIGHT"; break; case "9": keyOut = "NINE"; break; case "0": keyOut = "ZERO"; break; default: keyOut = keyChar; break; } try { //KeyCodes.ScanCode code = (KeyCodes.ScanCode)Enum.Parse(typeof(KeyCodes.ScanCode), keyOut); var query = from KeyCodes.keyPosition k in KeyCodes.keypositions where k.keyname == keyOut //(int)code select k; centroid c = new centroid(); c.point = new Point((int)query.First().x, (int)query.First().y); c.countup = 1; System.Drawing.Color c1 = Form1.m_startColour; System.Drawing.Color c2 = Form1.m_endColour; if (Form1.randomColours) { Random rndcol = new Random(); c1 = System.Drawing.Color.FromArgb(rndcol.Next(30, 255), rndcol.Next(30, 255), rndcol.Next(30, 255)); c2 = System.Drawing.Color.FromArgb(rndcol.Next(30, 255), rndcol.Next(30, 255), rndcol.Next(30, 255)); } ColorManagment.ColorConverter Converter = new ColorManagment.ColorConverter(); //create a new instance of a ColorConverter ColorRGB rgb1 = new ColorRGB(RGBSpaceName.sRGB, c1.R, c1.G, c1.B); //create an RGB color c.lab1 = Converter.ToLab(rgb1); ColorRGB rgb2 = new ColorRGB(RGBSpaceName.sRGB, c2.R, c2.G, c2.B); //create an RGB color c.lab2 = Converter.ToLab(rgb2); if (centroids.Count > 0) { for (int i = centroids.Count - 1; i > -1; i--) { Debug.WriteLine(i); if (centroids[i].point == c.point) { centroids.RemoveAt(i); break; } } } centroids.Add(c); //DoAnimation(); } catch (Exception ex) { Debug.WriteLine("AddKeyPress: handled exception: " + ex.Message); } }