We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › resorting an array
Page Index Toggle Pages: 1
resorting an array (Read 733 times)
resorting an array
May 24th, 2005, 7:01am
 
hi
i am,, trying to get the theory right for an instalation i'm doing, resorting values to create an outline (want to connect the points as a bezier).

say i have a number of x,y points generated in a 10x10 grid:
(5,1), (7,1), (3,2), (8,3), (4,4), (7,5), (2,6), (8,6)

since pixels are measured from 1 to 100, but i want to trace an outline.
starting from left to right, highest y to lowest y, until i get to middle and then reverse the process, sort from lowest y to highest y (picture tracing a human head).

so that's the theory, here's the code. all the sorting works, but at the end, doesn't insert it back into the array, been pulling my hair out, probably simple.

also, am i doing this in a long-winded way. is there a more simple solution.

////////////////////////////

int maxPoints;
int numLines=9;
int[] Xpoints = new int[1000];
int[] Ypoints = new int[1000];
int[] p = new int[1000];
int[] sY = new int[1000];
int[] pL = new int[1000];
int[] pR = new int[1000];
int[] pX = new int[1000];
int[] pY = new int[1000];
int smallestY=400;
int smallestX=400;
int count;
int myWidth=10;
int myHeight=10;

void setup() {
 size(100, 100);
 stroke(255,0,0);
////
Xpoints[0]=0;
Xpoints[1]=5;
Xpoints[2]=7;
Xpoints[3]=3;
Xpoints[4]=8;
Xpoints[5]=4;
Xpoints[6]=7;
Xpoints[7]=2;
Xpoints[8]=8;
 Xpoints[9]=10;
Ypoints[0]=10;
Ypoints[1]=1;
Ypoints[2]=1;
Ypoints[3]=2;
Ypoints[4]=3;
Ypoints[5]=4;
Ypoints[6]=5;
Ypoints[7]=6;
Ypoints[8]=6;
Ypoints[9]=10;
//// thiswill be used later
 for(int i=1; i<numLines; i++) {
 if (Ypoints [i]<smallestY){
 smallestY=Ypoints [i];
 }
  if (Xpoints [i]<smallestX){
 smallestX=Xpoints [i];
 }
 }

convertToPixels();
leftAndRight();
nowDrawLine();
}

void draw() {
//background(222);



}

void leftAndRight(){
// left
pX[0]=0;
pY[0]=0;
print (" left: ");
int count=0;
for (int i=100; i>0; i--){
//count++;
if(i%10<=5){
for (int j=1; j<numLines; j++){
if (p[j]==i){
pL[j]=p[j];
pX[j]=int(i%10);
pY[j]=int(round(i/myHeight));
print (" ; "+ pX[j] + ","+pY[j]);
stroke(255);
line(pX[j-1]*10, pY[j-1]*10, pX[j]*10,pY[j]*10);
break;
}
}
}
}
//////
// right
print (" right: ");
count=0;
for (int i=10; i<100; i++){
//count++;
if(i%10>5){
for (int j=1; j<numLines; j++){
if (p[j]==i){
pR[j]=p[j];
pX[j]=int(i%10);
pY[j]=int(round(i/myHeight));
print (" ; "+pX[j]+","+pY[j]);

break;
}
}
}
}

}


void convertToPixels(){
for (int i=1; i<numLines; i++){
p[i]=Xpoints [i]+(Ypoints [i])*myWidth;
}
}



void nowDrawLine(){
print ("/n Final output: ");
for (int j=1; j<numLines; j++){
print (pX[j] + ","+pY[j]+" ; ");
stroke(255,0,0);
line(pX[j-1]*10, pY[j-1]*10, pX[j]*10,pY[j]*10);
}
}
Page Index Toggle Pages: 1