Java Processing --> Plotting Two Curves on the same Plot

  • Thread starter Thread starter jedishrfu
  • Start date Start date
AI Thread Summary
The discussion focuses on a Java Processing sketch that effectively visualizes the sine and cosine functions over the interval [0, 2π]. The sketch sets up an 800x400 pixel window, calculating scaling factors for the x and y axes to fit the mathematical functions within the display area. It includes functions to draw the axes with appropriate labels for 0, π, and 2π, as well as for the y-values of 1 and -1. The sine function is represented in red, while the cosine function is shown in blue, both plotted using a continuous curve method. A notable correction was made regarding the initialization of global variables for width and height, ensuring the size() function is called correctly. The final output features a well-labeled graph that dynamically scales to fit the window, showcasing both functions smoothly.
Messages
15,441
Reaction score
10,138
Here is a Java Processing sketch that plots both the sine and cosine functions in the interval [0, 2\pi].

Processing makes it easy to create graphical representations of mathematical functions.

Processing Code:​


Java:
float scaleX;     // Scaling factor for x-axis
float scaleY;     // Scaling factor for y-axis
float padding = 50;

void setup() {
  size(800, 400);
  scaleX = (width - 2 * padding) / (2 * PI); // Scale x to fit from 0 to 2π
  scaleY = (height - 2 * padding) / 2;       // Scale y from -1 to 1
  background(255);
  drawAxes();
  drawSine();
  drawCosine();
}

void drawAxes() {
  stroke(0);
  line(padding, height / 2, width - padding, height / 2); // X-axis
  line(padding, padding, padding, height - padding);      // Y-axis
 
  // Labels
  fill(0);
  textSize(14);
  text("0", padding - 10, height / 2 + 15);
  text("π", padding + scaleX * PI - 5, height / 2 + 15);
  text("2π", padding + scaleX * 2 * PI - 10, height / 2 + 15);
  text("1", padding - 20, height / 2 - scaleY);
  text("-1", padding - 25, height / 2 + scaleY);
}

void drawSine() {
  stroke(255, 0, 0); // Red for sine
  noFill();
  beginShape();
  for (float x = 0; x <= 2 * PI; x += 0.01) {
    float screenX = padding + x * scaleX;
    float screenY = height / 2 - sin(x) * scaleY;
    vertex(screenX, screenY);
  }
  endShape();
}

void drawCosine() {
  stroke(0, 0, 255); // Blue for cosine
  noFill();
  beginShape();
  for (float x = 0; x <= 2 * PI; x += 0.01) {
    float screenX = padding + x * scaleX;
    float screenY = height / 2 - cos(x) * scaleY;
    vertex(screenX, screenY);
  }
  endShape();
}

Explanation:​


  1. Window Setup (setup()):
    • The window size is set to 800x400 pixels via the size() function call in setup(). Size() initializes the width and height global variables.
    • Scaling factors for both x and y axes are calculated based on the window size.
    • Calls functions to draw axes, sine, and cosine curves.
  2. Drawing Axes (drawAxes()):
    • The x-axis and y-axis are drawn with labels for 0, π, and 2π.
    • The y-axis is labeled at 1 and -1.
  3. Plotting the Sine Function (drawSine()):
    • Uses a red stroke.
    • Iterates over x-values from 0 to 2π, mapping function values to screen coordinates.
    • Uses beginShape() and vertex() to draw the continuous sine curve.
  4. Plotting the Cosine Function (drawCosine()):
    • Uses a blue stroke.
    • Similar approach to the sine function but with cos(x) instead of sin(x).

Notes:​

ChatGPT originally generated the java processing sketch incorrectly. It erroniously initialized two global integer variables width and height and used them as arguments for the size(width,height).

Its a known gotcha in processing coding. I removed the global definitions and replaced size(width, height) with size(800,400). Size() can only be called once at the start of a processing sketch.

https://processing.org/reference/size_.html

Output:​


  • A red sine wave and a blue cosine wave smoothly plotted over the interval [0, 2\pi].
  • Properly labeled x-axis and y-axis.
  • Graph is dynamically scaled to fit within the window.

Screenshot 2025-03-15 at 2.11.54 AM.png


Solution Credits​


Proposed by: @hagopbul
Coded by: @ChatGPT
Reviewed by: @jedishrfu
 
Last edited:
  • Like
Likes Greg Bernhardt and 256bits
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
0
Views
788
Replies
0
Views
309
Replies
4
Views
5K
Replies
1
Views
361
Replies
1
Views
2K
Back
Top