# A Pulsar Array. Need Help please

**URL:** <https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 15, 2020, 10:20am UTC](https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797 "2020-04-15T10:20:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)\
**Post date:** [April 15, 2020, 10:20am UTC](https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797/1 "2020-04-15T10:20:48Z")

</div>

Hey Guys,

this is one of the hardest tasks in the exam. There are another variations in the other exams which I will include in the next days in another topics.

Now This task in german

```auto
Schreiben	Sie	eine	Klasse	Pulsar,	die	einen	Kreis	malt,	der	sich	ausdehnt	und	
wieder	zusammenzieht	(also	pulsiert).	Die	Klasse	hat	5	Eigenschaften:	x/yPosition,	Geschwindigkeit	(der	Ausdehnung/Zusammenziehung,	als	float),	
Durchmesser	und	eine	boolesche	Variable	active.
Im	Konstruktor sollen	x/y	zufällig,	Durchmesser	auf	10	und	Geschwindigkeit	
zufällig	(zwischen	0.2	und	1.5)	gesetzt	werden.
Schreiben	Sie	zwei	Methoden fürs	Zeichnen	(render)	und	Updaten	(update).	
Der	Durchmesser	des	Kreises	soll	max.	30	und	min.	5	betragen. Wenn	die	Variable	active false	ist,	soll	
das	Objekt	nicht	pulsieren,	aber	gezeichnet	werden.
Im	Hauptprogramm erzeugen	Sie	ein	Fenster	der	Größe	100x100 mit	weißem	Hintergrund	und 100	
Pulsar-Objekten,	die	alle	gezeichnet	werden	und	pulsieren.
Zusätzlich	soll	per	Tastendruck die	Bewegung von	der	Hälfte aller	Objekte angehalten	werden	können.	
Beim	nochmaligen	Druck	wird	sie	fortgesetzt	usw.	(Verwenden	Sie	dazu	die	Instanzvariable	active).

```

At the moment my Code is this but it is not finished yet:

```auto
class Pulsar {
  float xpos;
  float ypos;
  float speed;
  float dia;
  boolean active;

  Pulsar(float x, float y, float s)
  {
    xpos = x*(random(0, 10));
    ypos = y*(random(0, 10));
    speed = s*(random(0.2, 1.5));
  }

  void render()
  {
    fill(255);
    ellipse(xpos, ypos, dia, dia);
    xpos++;
    ypos++;
  }

  void update()
  {
    if (xpos >= 30 && ypos >= 30)
    {
      xpos = -xpos;
      ypos = -ypos;
    }

    if (xpos <= 5 && ypos <= 5)
    {
      -xpos = xpos;
      -ypos = ypos;
    }
  }
}

```

```auto
Pulsar[] p = new Pulsar[100];

void setup()
{
 size(100,100); 
  
  
}

void draw()
{
  
  
  
}

```

At first my class should be mostly right (I think)  
What is missing is that I was not able to write the void keypressed section but I will do this when everything is right.  
Could someone controll the class section and say to me what I must exchange or include?  
After that I will show you the next try of this code.

Thank you guys. 😃

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 15, 2020, 10:35am UTC](https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797/2 "2020-04-15T10:35:50Z")

</div>

> [@gurki](#):
>
> At first my class should be mostly right (I think)

You should address some of the obvious errors before posting code:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bffc5a8b1a1e0d9a02ba892e7163580bf4791208.png)

---

<div class="post-metadata">

**Author:** ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)\
**Post date:** [April 15, 2020, 10:43am UTC](https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797/3 "2020-04-15T10:43:23Z")

</div>

Is this the only part where It needs to be fixed in the class?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 15, 2020, 11:34am UTC](https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797/4 "2020-04-15T11:34:02Z")

</div>

You are posting the homework incorrect. It looks distorted. Don’t format it as code but as a quote `" "` .

Like this - also translate it into English, because most people here speak English.

> Schreiben Sie eine Klasse Pulsar, die einen Kreis malt, der sich ausdehnt und   
> wieder zusammenzieht (also pulsiert). Die Klasse hat 5 Eigenschaften: x/yPosition, Geschwindigkeit (der Ausdehnung/Zusammenziehung, als float),   
> Durchmesser und eine boolesche Variable active.  
> Im Konstruktor sollen x/y zufällig, Durchmesser auf 10 und Geschwindigkeit   
> zufällig (zwischen 0.2 und 1.5) gesetzt werden.  
> Schreiben Sie zwei Methoden fürs Zeichnen (render) und Updaten (update).   
> Der Durchmesser des Kreises soll max. 30 und min. 5 betragen. Wenn die Variable active false ist, soll   
> das Objekt nicht pulsieren, aber gezeichnet werden.  
> Im Hauptprogramm erzeugen Sie ein Fenster der Größe 100x100 mit weißem Hintergrund und 100   
> Pulsar-Objekten, die alle gezeichnet werden und pulsieren.  
> Zusätzlich soll per Tastendruck die Bewegung von der Hälfte aller Objekte angehalten werden können.   
> Beim nochmaligen Druck wird sie fortgesetzt usw. (Verwenden Sie dazu die Instanzvariable active).

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 15, 2020, 11:42am UTC](https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797/5 "2020-04-15T11:42:15Z")

</div>

> [@gurki](#):
>
> Pulsar(float x, float y, float s) {  
> xpos = x\*(random(0, 10));  
> ypos = y\*(random(0, 10));  
> speed = s\*(random(0.2, 1.5)); }

Nach der Aufgabenstellung braucht der Konstruktor keine Parameter.

Du sollst xpos, ypos und speed nur zufällig festlegen. Du kannst es also einfacher machen.

**Andere Anmerkungen**

Du hast dia nicht auf 10 gesetzt

Das Pulsieren wird so nicht funktionieren, denke ich.

Das kannst du testen, wenn du die Klasse von draw aus auch benutzt. So wie es jetzt ist, kannst du die Klasse nicht testen, und wir auch nicht. Versuche nur Code zu posten, der ein lauffähiges Programm ist.

Chrisir

---

<div class="post-metadata">

**Author:** ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)\
**Post date:** [April 15, 2020, 7:06pm UTC](https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797/6 "2020-04-15T19:06:02Z")

</div>

Ich habe nun mein Code erweitert.  
Leider funktioniert es nicht.

Ich verstehe es nicht.  
Habe nun den Code auf das Hauptprogramm übertragen aber leider erscheint da gar nichts.  
Eigentlich müssten nun die Bälle aufblasen und kleiner werden -.-.

```auto
class Pulsar {
  float xpos;
  float ypos;
  float speed;
  float dia;
  boolean active;

  Pulsar()
  {
    xpos = (random(0, 10));
    ypos = (random(0, 10));
    speed = (random(0.2, 1.5));
  }

  void render()
  {
    fill(255);
    ellipse(xpos, ypos, dia, dia);
    xpos++;
    ypos++;
  }

  void update()
  {
    if (xpos >= 30 || ypos >= 30)
    {
      xpos = -xpos;
      ypos = -ypos;
    }

    if (xpos <= 5 || ypos <= 5)
    {
      xpos = -xpos;
      ypos = -ypos;
    }
  }
}

```

```auto
Pulsar[] p = new Pulsar[100];

void setup()
{
 size(100,100); 
 for(int i= 0; i < p.length; i++)
 {
   p[i] = new Pulsar();
 }
  
  
}

void draw()
{
  for(int i= 0; i < p.length; i++)
 {
   p[i].render();
   p[i].update();
 }
  
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 15, 2020, 8:42pm UTC](https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797/7 "2020-04-15T20:42:48Z")

</div>

Das Hauptprogramm ist gut.

Dein handling von xpos und ypos ist falsch.

Lies nochmal die Aufgabe; was genau soll pulsieren? Die Position???

---

<div class="post-metadata">

**Author:** ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)\
**Post date:** [April 17, 2020, 11:16am UTC](https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797/8 "2020-04-17T11:16:02Z")

</div>

Hey Guys,  
yesterday I was very busy with my work.

Now I have nearly completed this task.

Here is my code.  
I am very confident this time:

This is the main code

```auto
Pulsar[] p = new Pulsar[100];

void setup()
{
 size(100,100); 
 for(int i= 0; i < p.length; i++)
 {
   p[i] = new Pulsar();
 }
  
  
}

void draw()
{ background(255);
  for(int i= 0; i < p.length; i++)
 {
   p[i].render();
   p[i].update();
 }
  
}

```

This is the class code

```auto
class Pulsar {
  float xpos;
  float ypos;
  float speed;
  float dia;
  boolean active;

  Pulsar()
  {
    xpos = (random(0, 100));
    ypos = (random(0, 100));
    speed = (random(0.2, 1.5));
    dia = (random(5, 30));
  }

  void render()
  {
    fill(255);
    ellipse(xpos, ypos, dia, dia);

    dia++;
  }

  void update()
  {
    if (dia >= 30)
    {
      dia = -dia;
    }

    if (dia < 5)
    {
      dia = +dia;
    }
  }
}

```

Now i need to make the part with the boolean active.

If I use Keypressed half of the Pulsar balls need to stop.  
I think I need to make this in the class code.  
But I am not sure how.

The part in the class is not filled cause I dont know how:

```auto
void keyPressed()
{
  if(active == true)
  {
    
    
    
  }

}

```

After this is filled the task should be fine.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 17, 2020, 11:25am UTC](https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797/9 "2020-04-17T11:25:43Z")

</div>

> [@gurki](#):
>
> dia++; }
> 
> void update()  
> {  
> if (dia \>= 30) {  
> dia = -dia;  
> }
> 
> if (dia \< 5) {  
> dia = +dia; }
> 
> }

I am not happy with this.

You are working with a negative diameter, which works, but is more like a work-around. There is no such thing as a negative diameter in the real world.

Instead, remember the ball that gets reflected on the sides of the rect: `x = x + speedX;`

I would make the pulsar like the ball!

- Instead of `dia++;` better say `dia = dia + diaSpeed;` (or speed) (or `dia += diaSpeed;`). The var `diaSpeed` would be `1` initially or random like in the assignment. (One could argue, this belongs into the method update() instead of render(). Why?)

- Then instead of dia = -dia; better diaSpeed = - diaSpeed; (in both places in your code) (or speed)

**Es steht sogar in der Aufgabe**

> Geschwindigkeit (der Ausdehnung/Zusammenziehung, als float)

**da ist dein speed, den du aber nicht mehr benutzt!!!**

Warmest regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 17, 2020, 11:29am UTC](https://discourse.processing.org/t/a-pulsar-array-need-help-please/19797/10 "2020-04-17T11:29:08Z")

</div>

> [@gurki](#):
>
> If I use Keypressed half of the Pulsar balls need to stop.  
> I think I need to make this in the class code.

This should be in the class:

```auto
 if(active == true) // or just if(active) 
  {
    
    // what belongs here??? You already have this. 
    
  }

```

**Details**

in `keyPressed()` - a function outside the class - you want to for-loop over the array(!) and randomly switch half of the pulsars to inactive. E.g. with `if(random(100)<50)....` and then count the active ones and stop at `50`.

`active` must be inside the class because it is a property of the class and different for each pulsar (the objects derived from the class).

Chrisir
