Skip to content

Commit

Permalink
Update FunctionPlotter to draw primes with Erathostenes
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Morgenthal authored and Adrian Morgenthal committed Jul 27, 2024
1 parent de9a5be commit b08e13d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/rancore/math/Erathostenes.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static boolean[] sieve(int n) {
}

public static void main(String[] args) {
int n = 300;
int n = 3000;
boolean[] primes = sieve(n);
System.out.print("Primzahlen bis " + n + ": ");
for (int i = 2; i <= n; i++) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/rancore/math/plot/FunctionPlotter.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ protected void paintComponent(Graphics g) {
drawGrid(g2, width, height);
drawAxis(g2, height, width);
drawAxisScale(g2, width, height, scaleX, scaleY, rangeX, rangeY);
drawPrimes(g2, width, height);
drawSquared(g2, width, height);
drawFunction(g2, width, height);
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/rancore/math/plot/GraphComponent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.rancore.math.plot;

import org.rancore.math.Erathostenes;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -83,6 +85,20 @@ static void drawFunction(Graphics2D g2, int width, int height) {
}
}

static void drawPrimes(Graphics2D g2, int width, int height) {
g2.setColor(Color.RED);
int n = 300;
boolean[] primes = Erathostenes.sieve(n);
for (int i = 2; i <= n; i++) {
if (primes[i]) {

g2.drawLine(i, height /2, width+i, height / 2*i);
g2.drawLine(i, height *2, width+i, height * 2*i);
System.out.print(i + " ");
}
}
}

static void drawSquared(Graphics2D g2, int width, int height) {
// Draw the function y = x^2
g2.setColor(Color.RED);
Expand Down

0 comments on commit b08e13d

Please sign in to comment.