Processing 2. Креативное программирование
.pdf. , , superArray, expand().
println( "Increase the size of an array" );
println( "------------------------------ |
"); |
println( "Length |
before expanding: " + superArray.length ); |
superArray = expand( superArray ); |
|
// double length |
of array |
println( "Length |
after expanding: " + superArray.length); |
// expand array to length of 256 |
|
superArray = expand( superArray, 256 ); |
|
println( "Length |
after expanding: " + superArray.length ); |
println(); |
|
subset().- , substring().
println( "Extract elements from an array" ); |
||
println( "------------------------------ |
"); |
|
float[] shortArray = subset( superArray, 1, |
4 ); |
|
println( shortArray ); |
|
|
Of the last two functions we'll use, one is to reverse an array and another is to sort the numbers from small to big.
println( "Reverse the order of the array" );
println( "------------------------------ |
|
"); |
|
float[] reversed = |
reverse( shortArray ); |
||
println( reversed ); |
|
||
println( |
"Sort the |
values of the array" ); |
|
println( |
"------------------------------ |
|
"); |
float[] sorted = sort( shortArray ); println( sorted );
.
New Array: Array 1 + new float
------------------------------
[0]1.0
[1]4.7
[2]3.08
[3]127.75
Copied Array 2 to New Array
100
4
------------------------------
[0]1.0
[1]4.7
[2]72.86
[3]48.32
Add array2 to end of array1
------------------------------
[0]1.0
[1]4.7
[2]3.08
[3]72.86
[4]48.32
Increase the size of an array
------------------------------
Length before expanding: 5 Length after expanding: 10 Length after expanding: 256 Extract elements from an array
------------------------------
[0]4.7
[1]3.08
[2]72.86
[3]48.32
Reverse the order of the array
------------------------------
[0]48.32
[1]72.86
[2]3.08
[3]4.7
Sort the values of the array
------------------------------
[0]3.08
[1]4.7
[2]48.32
[3]72.86
, ..
fappend() . - , ,, , , . , .
101
farrayCopy(). . ,. -,..
f, concat().
fexpand() ., ., , .
fsubset() ., ,, ,.
freverse() .
fsort() . , - , , ., .
ArrayList
, -. .ArrayList ,.
working_with_arraylists.pde. , - MyObject.PDE
Shift + Cmd + N Mac Shift + Ctrl + N Windows Linux.
102
4
. -ArrayList.
class MyObject
{
float x; float y;
MyObject()
{
x = random( width ); y = random( height );
}
void update()
{
y--;
}
void render()
{
ellipse( x, y, 60, 60 );
}
}
103
MyObject working_ with_arraylists setup().
ArrayList<MyObject> myList;
void setup()
{
size( 640, 480 ); smooth();
myList = new ArrayList<MyObject>(); for ( int i = 0; i < 4; i++ ) {
myList.add( new MyObject() );
}
}
draw() , -. ,.
void draw()
{
background( 255 );
fill( 255, 128 ); stroke( 0 );
for ( int i = myList.size() - 1; i >= 0; i-- ) { MyObject o = (MyObject)myList.get( i ); o.update();
o.render();
if ( o.y <= 0 ) { myList.remove( i );
}
}
}
, - mousePressed().,.
void mousePressed()
{
myList.add( new MyObject() );
println( "Total elements in List: " + myList.size() );
}
104
4
, :
, - ArrayList setup().:
ArrayList myList;
, ,. MyObject.
ArrayList<MyObject> myList;
setup() ArrayList. ,.
myList = new ArrayList(); // without datatype myList = new ArrayList<MyObject>(); // with datatype
105
add() .for .:
myList.add( new MyObject() );
remove() ..myList.remove( 0 ).
ArrayList.ArrayList , setup(), (myList = new ArrayList<MyObject>();), for...
for ( MyObject o : myList ) { o.update();
o.render();
}
ArrayList - .iterator() ArrayList. hasNext() (true), , (false), - .next() .
Iterator itr = myList.iterator(); while ( itr.hasNext() ) {
MyObject o = (MyObject)itr.next(); o.update();
o.render();
}
HashMap
HashMap , ., HashMapString. HashMap ,.
106
4
HashMap setup().
HashMap<String, Float> hm;
void setup()
{
hm = new HashMap<String, Float>(); hm.put("Processing", 51.30); hm.put("openFrameworks", 30.45); hm.put("Cinder", 12.78);
noLoop();
}
, draw() HashMap.
Iterator i = hm.entrySet().iterator(); while ( i.hasNext () ) {
Map.Entry me = (Map.Entry)i.next();
println( "Key: " + me.getKey() + ", Value: " + me.getValue() );
}
println("---");
, HashMap, isEmpty(). HashMap get().
println( "Is Empty? " + hm.isEmpty() );
println( "Get 'Processing': " + hm.get("Processing") );
HashMap size()., ArrayList. HashMap remove().
println( "Number of Elements (before remove): " + hm.size() ); println( "Removed: " + hm.remove("openFrameworks") ); println( "Number of Elements (after remove): " + hm.size() );
, - HashMapcontainsKey().
println( "Contains key 'openFrameworks': " + hm.containsKey("openFram eworks") );
107
, :
Key: Cinder, Value: 12.78 Key: Processing, Value: 51.3
Key: openFrameworks, Value: 30.45
---
Is Empty? false
Get 'Processing': 51.3
Number of Elements (before remove): 3 Removed: 30.45
Number of Elements (after remove): 2 Contains key 'openFrameworks': false
HashMap. , . , HashMap . float int.HashMap Integer Float Java.Integer int; Floatfloat.
Hashmap<String, Float> hm;
hm = new HashMap<String, Float>();
put(). , -String, . -. Float.remove(). ,, .
HashMap isEmpty().true HashMap false . , HashMap , -containsKey(). , , , ..
HashMap . ArrayList. : hm.entrySet().iterator().while : Map.Entry me = (Map. Entry) itr.next(). , getKey() getValue().
108
5
Processing
:
fPDF
fPDF
f3D
., -. Processing , ., .
- FlickrFacebook. , , ,. ProcessingsaveFrame().