Nico's Projects

Workplace to make some stuff

Kmeans Library

You can test this project directly in your browser. Just click here

Setting up:

  1. Download the kmeans.js file from here: download me
  2. Reference the file in your html file
  3. Reference the p5.js Library in your html file. You can download the p5.js Library from here: p5js.org
  4. Copy following Code into your javascript file
  //Variable holding the kmean object
  let k;

  //Number of Clusters
  let nc = 2;

  //Number of Spots
  let ns = 200;

  //create a new kmean object
  k = new kmeans(nc, ns);

  //initialize the kmean object
  k.initialize();

Functions

Add new Spots

//Positions
let x = 100;
let y = 100;

//if you want to add more than one spot, you can set an optional parameter
let morethanone = 4;

//Call .addSpots to add a new Spot
k.addSpots(x, y, morethanone);

Add new Nodes

//the Position of the Node will be randomly picked
k.addNode();

Example

let kmean;

function setup() {
  createCanvas(windowWidth, windowHeight);
  kmean = new kmeans(20, 40);
  kmean.initialize();
};

function mousePressed() {
  kmean.addSpots(mouseX, mouseY);
}

function mouseDragged() {
  kmean.addSpots(mouseX, mouseY);
}

Screenshots

Screenshot Screenshot_1

Back