I put this code in the draw() function in Processing, and it doesn't work. Can someone explain why, and help me fix this? What I want it to do is to cycle through each element of the 2d boolean array and check if it is true or false.
for(int i = 0; i < elemts.length; i++)
{
for(int j = 0; j < elemts[0].length; j++)
{
if(elemts[i][j] == true)
{
rect(i*5,j*5,5,5);
}
}
}
The for loop should be like this
for(start; condition; code to excute every turn)
When you want to skip it you should add a semicolon.
First of all, you didn't add i++ or j++, so the for-loop doesn't run. Next, the draw function runs itself 60 times per second, so putting a for-loop with a certain number of elements doesn't allow the draw() to work. Use the below code instead.
if(elemts[x][y] == true)
{
rect(x*5,y*5,5,5);
fill(0,0,0);
}
if(x < elemts.length)
{
x += 1;
}
if(x == elemts.length)
{
x = 0;
y += 1;
}
Related
so its my first day using processing and i need a bit of help to start with this is my code, im doing the selection sort:
int[] numbers; // Declare array
int currentmin;
int exchange=0;
void setup() {
frameRate(0.1);
size(500,500);
numbers = new int[10]; // Create array with 10 cells
background(105);
for (int i = 0; i < numbers.length; i++) { // random numbers from 1 to 100
int r = int(random(1,100)); // create random numbers
for (int j=0; j<numbers.length; j++) { //make sure not duplications
if(numbers[j]==r)
{r=r+1;}
}
numbers[i]=r; //fill array with random numbers
println(r);
}
fill(255,0,0);
for (int i = 0; i < numbers.length; i++) { //draw rectangles
rect(i*40+10,400 ,35, -numbers[i]);
}
for (int j=0; j < numbers.length; j++){ //set pivot number
currentmin=numbers[j];
for (int i=j+1; i < numbers.length; i++){ //find lowest number in array
if (numbers[i] < currentmin) {
currentmin = numbers[i];
}
}
for ( int i=j; i<numbers.length;i++){ //swap pivot with lowest number
if ( numbers[i]==currentmin){
exchange=numbers[j];
numbers[j]=numbers[i];
numbers[i]=exchange;
//here
}
}
}
}
void draw() {
background(105);
for (int z = 0; z < numbers.length; z++) {
rect(z*40+10,400 ,35, -numbers[z]);
}
}
as you can see where i have the comment here im trying to make the thing animated and to see the rectangles change place on each iteration in the first loop, but its not working... any help ? i tried calling draw(); hopelessly but it didnt work... anyway i can get a help ?
This isn't exactly a trivial animation to execute. You should really start with something simpler.
But to answer your question, you wouldn't put your animation inside a for loop. Instead, you need to make it so the draw() function draws a single frame of the animation. Processing automatically calls the draw() function 60 times per second, so by changing what's drawn each time, you create an animation.
Shameless self-promotion: I wrote a tutorial on creating animations in Processing available here.
I have 3 lists: ListOfBoxes, ListOfGoals and ListOfDoned.
these first 2 lists contain transforms, the 3rd is empty.
When a box from ListOfBoxes x and z position are the same as a goals in ListOfGoals i move that box to ListOfDoned.
What I want to happen next is; If a box in the ListOfDoned is no longer at a goal in ListOfGoals move back into ListOfBoxes.
Here Is My Code:
for(var n = 0; n < ListOfGoals.Count; n++)
{
for (var p = 0; p < ListOfBoxes.Count; p++)
{
if (Mathf.Abs(ListOfBoxes[p].position.x - ListOfGoals[n].position.x) < epsilon)
{
if (Mathf.Abs(ListOfBoxes[p].position.z - ListOfGoals[n].position.z) < epsilon)
{
Debug.Log("Box" + p + " has been placed at " + n);
ListOfDoned.Add(ListOfBoxes[p]);
ListOfBoxes.RemoveAt (p);
break;
}
}
}
}
for (var q = 0; q < ListOfDoned.Count; q++)
{
for (r = 0; r < ListOfGoals.Count; r++)
{
if (Mathf.Abs(ListOfDoned[q].position.x - ListOfGoals[r].position.x) < epsilon)
{
if (Mathf.Abs(ListOfDoned[q].position.z - ListOfGoals[r].position.z) < epsilon)
{
break;
}
else
{
if (r == ListOfGoals.Count)
{
ListOfBoxes.Add(ListOfDoned[q]);
ListOfDoned.RemoveAt (q);
}
}
}
}
}
I can see that what is happening is that it's checking all of the goals in the list instead of exiting if it has found one, but I'm not sure what the logic would be go get this working correctly.
Thanks in advance.
[EDIT to try to add clarity] - Imagine I have 2 boxes in ListOfBoxes and 2 goals in listofgoals. When 1 box moves to the same position as one of the goals (only x and z axis, not y) I want that box removed from ListOfBoxes to ListOfDoned. That's the first nested for loop, and it seems to be working fine. However if that box is later moved from the same position as one of those goals I need it back in ListOfBoxes. This is what I want the second nested for loop to do.
What appears to be happening is that it is comparing the boxes in listOfDoned with all of the goals in listofgoals and (even though it has the same position as one of the goals) it doesnt have the same position as the other one so the else case and put the box in the list listofboxes.
I need to check against the position of all the goals, and as long as it's position is the same as one of them, don't do anything, else return it to listOfBoxes.
[EDIT: This answer addresses what I believe is a problem in your code, but I don't think it fixes the behaviour you describe in your comments. Let me revise this pending more clarification.]
I believe the problem you describe is due to your if statement inside the second nested loop:
if (r == ListOfGoals.Count)
{
ListOfBoxes.Add(ListOfDoned[q]);
ListOfDoned.RemoveAt (q);
}
You need to remember that the contents of your loop will only execute if the condition you specified in your for loop is true. In this case, that is
r < ListOfGoals.Count
This means that your if condition r == ListOfGoals.Count can never occur, because the loop will terminate before r ever reaches up to ListOfGoals.Count. Since the condition never evaluates to true, items will never be moved from ListOfDoned to ListOfBoxes, even if they're supposed to be.
To fix this, I suggest changing your if statement to:
if (r == ListOfGoals.Count - 1)
{
ListOfBoxes.Add(ListOfDoned[q]);
ListOfDoned.RemoveAt (q);
}
Which would correspond to the last execution of the loop. Hope this helps! Let me know if you have any questions.
Thanks to PipsqueakGames on twitter for helping me reach this solution.
for(var n = 0; n < ListOfGoals.Count; n++)
{
for (var p = 0; p < ListOfBoxes.Count; p++)
{
if (Mathf.Abs(ListOfBoxes[p].position.x - ListOfGoals[n].position.x) < epsilon)
{
if (Mathf.Abs(ListOfBoxes[p].position.z - ListOfGoals[n].position.z) < epsilon)
{
Debug.Log("Box" + p + " has been placed at " + n);
ListOfDoned.Add(ListOfBoxes[p]);
ListOfBoxes.RemoveAt (p);
break;
}
}
}
}
for (q = 0; q < ListOfDoned.Count; q++)
{
var matched:boolean = false;
for (r = 0; r < ListOfGoals.Count; r++)
{
if (Mathf.Abs(ListOfDoned[q].position.x - ListOfGoals[r].position.x) < epsilon)
{
if (Mathf.Abs(ListOfDoned[q].position.z - ListOfGoals[r].position.z) < epsilon)
{
matched = true;
Debug.Log("Box" + q + " is still at " + r);
break;
}
}
}
if (!matched)
{
ListOfBoxes.Add(ListOfDoned[q]);
ListOfDoned.RemoveAt (q);
}
}
This is what I have got but it doesn't work. When I try to compile I get this error message:
int result = 0;
^^^^^^^^^^^^^^^
Unreachable code
My code:
public int sumOfOddIntegers (int n) {
if(n < 1);
return 0;
int result = 0;
for(int i = n - 1; i > 0; i--)
{
if(i % 2 != 0) {
result = result + i;
}
}
return result;
}
if(n < 1);
return 0;
is equivalent to :
if(n < 1) {
}
return 0;
It shoud be replaced by :
if(n < 1)
return 0;
or (the right way)
if(n < 1) {
return 0;
}
The statement:
if(n < 1);
Is a no op because of the semi-colon. The comparison is evaluated, and nothing is done, whatever the result of the comparison is.
Then, the next line is executed, which returns 0.
if(n < 1); is your problem. The rest of the code is unreachable beacuse the following return' is always exectued.
Remove the ; after if(n < 1).
As others have said, the semi colon on your if statement is the problem. Personally however I would just do it like this:
public int sumOfOddIntegers (int n)
{
int result = 0;
if(n < 1)
return result;
for(int i = 1; i <= n; i += 2)
{
result += i;
}
return result;
}
This way you can halve the number of iterations. We know every other number is odd, so why even bother iterating the even ones and checking if they're odd when we know they're not?
the sequence is a arithmetic progression with common difference of 2.
so its sum would be given by formula :
sum = n/2(2a+(n-1)d
where n = Math.ceil(k); where k is the given number.
and d = 2, a=1
public int sumOfOddIntegers (int n) {
if(n < 1);
return 0;
int totalNumber = Math.ceil(n/2);
return (totalNumber/2)*(2 + (totalNumber-1)*2);
`
I'm trying to compare all elements of an array to each other. I can do it with nested loops but it's a very inefficient algorithm and I can tell it's not the right way to do it. Here's what I'm doing right now.
PER answers below I've changed this code and I'm expanding on the question.
// Point from java.awt.Point;
private static void findShortestDistance(Point[] pt) {
ArrayList<Double> distance = new ArrayList<Double>(1000);
for(int i=0; i<pt.length; i++) {
for(int j=i+1; j<pt.length; j++) {
double tmp = pt[i].distance(pt[j]);
distance.add(tmp);
}
}
double min = distance.get(0);
for(Double d : distance) {
if(d < min) { min = d; }
}
}
There's the full code for the method I have so far. I'm trying to find the shortest distance between two points in the given array.
Have a look at wikipedia.
http://en.wikipedia.org/wiki/Closest_pair_of_points
And this question seems to be the same as
Shortest distance between points algorithm
As long as you say the above code is exactly what you want the following will be the implementation you want:
for(int i=0; i<pt.length; i++) {
for(int j = i + 1; j<pt.length; j++) {
/* do stuff */
}
}
However, I have a god feeling you are actually interested in comparing the array values or am I wrong?
for(int i=0; i<pt.length; i++) {
for(int j=i+1; j<pt.length; j++) {
if(pt[i] != pt[j]) { /* do stuff */ }
}
}
for(int i=0; i<pt.length; i++) {
for(int j=i; j--; ) {
{ /* do stuff */ }
}
}
And, for completeness:
for(int i=pt.length; i--; ) {
for(int j=i; j--; ) {
{ /* do stuff */ }
}
}
Initializing j to i+1 will skip the redundant comparisons.
for(int i = 0; i < pt.length; i++) {
for(int j = i + 1; j < pt.length; j++) {
if(pt[i] != pt[j]) { /* do stuff */ }
}
}
(BTW, changed the if-statement, assuming you meant to use i and j as indexes into an array.)
That's about the best you can get for the most basic case where you need to get a cross product of the array with itself, minus all the elements {x, y} | x == y. (i.e. an exhaustive list of unordered pairs of differing elements.) If you need ordered pairs of differing elements, then your code is best.
I would like to separate objects in OpenCv like the following image it shows:
But if I am using cvDilate or cvErode the objects grow together... how to do that with OpenCv?
It looks like you will need to write your own dilate function and then add xor functionality yourself.
Per the opencv documentation, here is the rule that cvdilate uses:
dst=dilate(src,element): dst(x,y)=max((x',y') in element))src(x+x',y+y')
Here is pseudocode for a starting point (this does not include xor code):
void my_dilate(img) {
for(i = 0; i < img.height; i++) {
for(j = 0; j < img.width; j++) {
max_pixel = get_max_pixel_in_window(img, i, j);
img.pixel(i,j) = max_pixel;
}
}
}
int get_max_pixel_in_window(img, center_row, center_col) {
int window_size = 3;
int cur_max = 0;
for(i = -window_size; i <= window_size; i++) {
for(j = -window_size; j <= window_size; j++) {
int cur_col = center_col + i;
int cur_row = center_row + j;
if(out_of_bounds(img, cur_col, cur_row)) {
continue;
}
int cur_pix = img.pixel(center_row + i, center_col + j);
if(cur_pix > cur_max) {
cur_max = cur_pix;
}
}
}
return cur_max;
}
// returns true if the x, y coordinate is outside of the image
int out_of_bounds(img, x, y) {
if(x >= img.width || x < 0 || y >= img.height || y <= 0) {
return 1;
}
return 0;
}
As far as I know OpenCV does not have "dilation with XOR" (although that would be very nice to have).
To get similar results you might try eroding (as in 'd'), and using the eroded centers as seeds for a Voronoi segmentation which you could then AND with the original image.
after erosion and dilate try thresholding the image to eliminate weak elements. Only strong regions should remain and thus improve the object separation. By the way could you be a little more clear about your problem with cvDilate or cvErode.