ported updates & fixes from java mode · codeanticode/processing-android@4bc8ab2 · GitHub
Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit 4bc8ab2

Browse files
committed
ported updates & fixes from java mode
1 parent 28e7480 commit 4bc8ab2

12 files changed

Lines changed: 690 additions & 583 deletions

File tree

core/src/assets/shaders/LineVert.glsl

Lines changed: 3 additions & 1 deletion

core/src/processing/core/PGraphics.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,11 +2505,10 @@ public void sphere(float r) {
25052505
* endShape();</PRE>
25062506
*/
25072507
public float bezierPoint(float a, float b, float c, float d, float t) {
2508-
float t1 = 1.0f - t;
2509-
return a*t1*t1*t1 + 3*b*t*t1*t1 + 3*c*t*t*t1 + d*t*t*t;
2508+
float t1 = t-1.0f;
2509+
return t * ( 3*t1*(b*t1-c*t) + d*t*t ) - a*t1*t1*t1;
25102510
}
25112511

2512-
25132512
/**
25142513
* Provide the tangent at the given point on the bezier curve.
25152514
* Fix from davbol for 0136.

core/src/processing/data/FloatDict.java

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2-
3-
/*
4-
Part of the Processing project - http://processing.org
5-
6-
Copyright (c) 2013-16 The Processing Foundation
7-
8-
This library is free software; you can redistribute it and/or
9-
modify it under the terms of the GNU Lesser General Public
10-
License as published by the Free Software Foundation, version 2.
11-
12-
This library is distributed in the hope that it will be useful,
13-
but WITHOUT ANY WARRANTY; without even the implied warranty
14-
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15-
See the GNU Lesser General Public License for more details.
16-
17-
You should have received a copy of the GNU Lesser General
18-
Public License along with this library; if not, write to the
19-
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20-
Boston, MA 02111-1307 USA
21-
*/
22-
231
package processing.data;
242

253
import java.io.*;
@@ -45,7 +23,7 @@ public class FloatDict {
4523
protected float[] values;
4624

4725
/** Internal implementation for faster lookups */
48-
private HashMap<String, Integer> indices = new HashMap<String, Integer>();
26+
private HashMap<String, Integer> indices = new HashMap<>();
4927

5028

5129
public FloatDict() {
@@ -137,6 +115,31 @@ public int size() {
137115
}
138116

139117

118+
/**
119+
* Resize the internal data, this can only be used to shrink the list.
120+
* Helpful for situations like sorting and then grabbing the top 50 entries.
121+
*/
122+
public void resize(int length) {
123+
if (length == count) return;
124+
125+
if (length > count) {
126+
throw new IllegalArgumentException("resize() can only be used to shrink the dictionary");
127+
}
128+
if (length < 1) {
129+
throw new IllegalArgumentException("resize(" + length + ") is too small, use 1 or higher");
130+
}
131+
132+
String[] newKeys = new String[length];
133+
float[] newValues = new float[length];
134+
PApplet.arrayCopy(keys, newKeys, length);
135+
PApplet.arrayCopy(values, newValues, length);
136+
keys = newKeys;
137+
values = newValues;
138+
count = length;
139+
resetIndices();
140+
}
141+
142+
140143
/**
141144
* Remove all entries.
142145
*
@@ -145,7 +148,15 @@ public int size() {
145148
*/
146149
public void clear() {
147150
count = 0;
148-
indices = new HashMap<String, Integer>();
151+
indices = new HashMap<>();
152+
}
153+
154+
155+
private void resetIndices() {
156+
indices = new HashMap<>(count);
157+
for (int i = 0; i < count; i++) {
158+
indices.put(keys[i], i);
159+
}
149160
}
150161

151162

@@ -183,8 +194,8 @@ public void remove() {
183194
}
184195

185196
public Entry next() {
197+
++index;
186198
Entry e = new Entry(keys[index], values[index]);
187-
index++;
188199
return e;
189200
}
190201

@@ -368,6 +379,15 @@ public void set(String key, float amount) {
368379
}
369380

370381

382+
public void setIndex(int index, String key, float value) {
383+
if (index < 0 || index >= count) {
384+
throw new ArrayIndexOutOfBoundsException(index);
385+
}
386+
keys[index] = key;
387+
values[index] = value;
388+
}
389+
390+
371391
/**
372392
* @webref floatdict:method
373393
* @brief Check if a key is a part of the data structure
@@ -427,7 +447,7 @@ public void div(String key, float amount) {
427447
private void checkMinMax(String functionName) {
428448
if (count == 0) {
429449
String msg =
430-
String.format("Cannot use %s() on an empty %s.",
450+
String.format("Cannot use %s() on an empty %s.",
431451
functionName, getClass().getSimpleName());
432452
throw new RuntimeException(msg);
433453
}
@@ -544,6 +564,27 @@ public float maxValue() {
544564
}
545565

546566

567+
public float sum() {
568+
double amount = sumDouble();
569+
if (amount > Float.MAX_VALUE) {
570+
throw new RuntimeException("sum() exceeds " + Float.MAX_VALUE + ", use sumDouble()");
571+
}
572+
if (amount < -Float.MAX_VALUE) {
573+
throw new RuntimeException("sum() lower than " + -Float.MAX_VALUE + ", use sumDouble()");
574+
}
575+
return (float) amount;
576+
}
577+
578+
579+
public double sumDouble() {
580+
double sum = 0;
581+
for (int i = 0; i < count; i++) {
582+
sum += values[i];
583+
}
584+
return sum;
585+
}
586+
587+
547588
public int index(String what) {
548589
Integer found = indices.get(what);
549590
return (found == null) ? -1 : found.intValue();
@@ -717,10 +758,7 @@ public void swap(int a, int b) {
717758
s.run();
718759

719760
// Set the indices after sort/swaps (performance fix 160411)
720-
indices = new HashMap<String, Integer>();
721-
for (int i = 0; i < count; i++) {
722-
indices.put(keys[i], i);
723-
}
761+
resetIndices();
724762
}
725763

726764

@@ -730,10 +768,7 @@ public void swap(int a, int b) {
730768
* @return a FloatDict with the original keys, mapped to their pct of the total
731769
*/
732770
public FloatDict getPercent() {
733-
double sum = 0;
734-
for (int i = 0; i < count; i++) {
735-
sum += values[i];
736-
}
771+
double sum = sum();
737772
FloatDict outgoing = new FloatDict();
738773
for (int i = 0; i < size(); i++) {
739774
double percent = value(i) / sum;
@@ -791,4 +826,4 @@ public String toJSON() {
791826
public String toString() {
792827
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
793828
}
794-
}
829+
}

core/src/processing/data/IntDict.java

Lines changed: 69 additions & 35 deletions

0 commit comments

Comments
 (0)