Skip to content

Instantly share code, notes, and snippets.

@n1ckfg
Last active November 7, 2017 16:56
Show Gist options
  • Save n1ckfg/3875991 to your computer and use it in GitHub Desktop.
Save n1ckfg/3875991 to your computer and use it in GitHub Desktop.
Favorite Processing functions
float getAngle(PVector p1, PVector p2){
PVector a = PVector.sub(p2,p1);
float aa = abs(degrees(atan2(a.x,a.y)) - 180);
return aa;
}
String setOnOff(boolean _b){
String s;
if(_b){
s = "ON";
}else{
s = "OFF";
}
return s;
}
String chooseFileDialog() {
// requires import javax.swing.JFileChooser;
String choosePath = "";
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(chooser.getAcceptAllFileFilter());
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
//choosePath = "" + chooser.getSelectedFile().getName();
choosePath = "" + chooser.getSelectedFile().getPath();
}
catch(Exception e) {
}
}
return choosePath;
}
String zeroPadding(int _val, int _maxVal){
String q = ""+_maxVal;
return nf(_val,q.length());
}
void rotateXYZ(float _x, float _y, float _z){
rotateX(radians(_x));
rotateY(radians(_y));
rotateZ(radians(_z));
}
float rounder(float _val, float _places){
_val *= pow(10,_places);
_val = round(_val);
_val /= pow(10,_places);
return _val;
}
//--
String rgbToHex(color c){
return "#" + hex(c,6);
}
//-------------------------------
//gets the number of files in a folder
int countFolder(String path) {
File dataFolder = new File(path);
int numFiles = dataFolder.list();
return numFiles;
}
//-------------------------------
//simplifies the unnecessarily complex blend command; image, x, y, width, height, center/corner
void blendImage(PImage bI, int pX, int pY, String b, boolean center) {
String[] blendModes = { "BLEND", "ADD", "SUBTRACT", "LIGHTEST", "DARKEST", "DIFFERENCE", "EXCLUSION", "MULTIPLY", "SCREEN", "OVERLAY", "HARD_LIGHT", "SOFT_LIGHT", "DODGE", "BURN" };
int[] blendModeCodes = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
for(int i=0;i<blendModes.length;i++) {
if(b==blendModes[i]) {
int qX,qY;
if(center) {
qX=pX-(bI.width/2);
qY=pY-(bI.height/2);
} else {
qX=pX;
qY=pY;
}
blend(bI,0,0,bI.width,bI.height,qX,qY,bI.width,bI.height,blendModeCodes[i]);
}
}
}
//-------------------------------
//Tween movement. start, end, ease (more = slower).
float tween(float v1, float v2, float e) {
v1 += (v2-v1)/e;
return v1;
}
PVector tween3D(PVector v1, PVector v2, PVector e) {
v1.x += (v2.x-v1.x)/e.x;
v1.y += (v2.y-v1.y)/e.y;
v1.z += (v2.z-v1.z)/e.z;
return v1;
}
float shake(float v1, float s) {
v1 += random(s) - random(s);
return v1;
}
float boundary(float v1, float vMin, float vMax) {
if (v1<vMin) {
v1 = vMin;
}
else if (v1>vMax) {
v1=vMax;
}
return v1;
}
float gravity(float v1, float v2, float v3) { //y pos, floor num, gravity num
if (v1<v2) {
v1 += v3;
}
if (v1>v2) {
v1 = v2;
}
return v1;
}
//-------------------------------
//2D Hit Detect. Assumes center. x,y,w,h of object 1, x,y,w,h, of object 2.
boolean hitDetect(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) {
w1 /= 2;
h1 /= 2;
w2 /= 2;
h2 /= 2;
if(x1 + w1 >= x2 - w2 && x1 - w1 <= x2 + w2 && y1 + h1 >= y2 - h2 && y1 - h1 <= y2 + h2) {
return true;
}
else {
return false;
}
}
//3D Hit Detect. Assumes center. xyz, whd of object 1, xyz, whd of object 2.
boolean hitDetect3D(PVector p1, PVector s1, PVector p2, PVector s2) {
s1.x /= 2;
s1.y /= 2;
s1.z /= 2;
s2.x /= 2;
s2.y /= 2;
s2.z /= 2;
if ( p1.x + s1.x >= p2.x - s2.x &&
p1.x - s1.x <= p2.x + s2.x &&
p1.y + s1.y >= p2.y - s2.y &&
p1.y - s1.y <= p2.y + s2.y &&
p1.z + s1.z >= p2.z - s2.z &&
p1.z - s1.z <= p2.z + s2.z
) {
return true;
}
else {
return false;
}
}
//-------------------------------
float angle(float x1, float y1, float x2, float y2) {
return tan(abs(x1-x2)/abs(y1-y2));
}
//-------------------------------
//Easy randomizer. range,threshold
boolean rollDice(float v1, float v2) {
float rollDiceNum;
rollDiceNum = random(v1);
if(rollDiceNum<v2) {
return true;
}
else {
return false;
}
}
//--- END ---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment