Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
How do you stretch a sine graph while retaining resolution in p5.js?

I have written a sine graph plotting program. It works apart from the fact that the graph is choppy. I need to make it so that I can stretch the line without changing the x values of the points.

Here is the project: https://editor.p5js.org/korczaka6052/sketches/49-sJW6wz

let slidery;
let sliderx
width_ = 800
height_ = 400
linelen = width_ - 100

function setup() {
  createCanvas(width_, height_);
  //create sliders controlling xstep and ystep
  slidery = createSlider(0, 100);
  slidery.position(10, 10);
  slidery.style('width', '200px');
  sliderx = createSlider(0, 100);
  sliderx.position(250, 10);
  sliderx.style('width', '200px');
}


class createPoint { //create a point with its own x and y coordinate, ik could use vectors
  constructor(x_, y_) {
    this.x = 50 + x_;
    this.y = height_ / 2 - y_;
  }
  show() {
    fill(0)
    circle(this.x, this.y, 1)
  }
  writetext() {
    textSize(10)
    text(this.x, this.x, height / 2 + 10)
  }
}

//where all the points will be stored
points = [];
xstep = 1;
ystep = 50;
looper = 0;

function draw() {
  //set xstep and ystep to their slider values
  ystep = slidery.value()
  xstep = sliderx.value()
  stroke(0)
  background(220);

  //create graph lines
  line(50, height / 2, width - 50, height / 2);
  line(50, 50, 50, height - 50);

  //for every [ystep] pixels calculate y based off equation
  for (i = 0; i < 800; i++) {
    points[i] = new createPoint(i * xstep, sin(i) * ystep); //creates that point as object with x and y value, y = sin(x) 
  }

  //create line between current and previous point
  for (i = 1; i < 800; i++) {
    stroke(255, 0, 0)
    //create only if point is inside canvas
    if (points[i - 1].y < height) {
      line(points[i - 1].x, points[i - 1].y, points[i].x, points[i].y)
    }
  }

  //create white borders to cut off extreme points
  noStroke(0)
  fill(220)
  rect(width - 50, 0, width, height)
  rect(width, height, width - 50, 0)
  rect(0, 0, width, 50)
  rect(0, height, width, -50)
  looper++
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

I am relatively new to p5.js so please excuse bad code.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The reason your line is choppy is because when xstep is greater than 1, the horizontal distance between each pair of points is relatively large. When you start out with an xstep of 50 you may have 800 points but only the first 16 or so are actually on the screen. Instead of advancing your x coordinate by xstep you should just advance by 1 (i.e., use i for the first argument to createPoint instead of i * xstep). When you do so, in order to draw the desired curve, you need to change the input to the sin() function used to calculate the corresponding y value. Because when you move forward by one pixel you are only moving to the right by 1 / xstep in the graph coordinate system, you simply need to use sin(i / xstep) instead of sin(i).

let slidery;
let sliderx;
let smooothCheckbox;
const width_ = 800;
const height_ = 400;
const linelen = width_ - 100;

function setup() {
  createCanvas(width_, height_);
  //create sliders controlling xstep and ystep
  slidery = createSlider(0, 100);
  slidery.position(10, 10);
  slidery.style('width', '200px');
  sliderx = createSlider(0, 100);
  sliderx.position(250, 10);
  sliderx.style('width', '200px');
  
  smoothCheckbox = createCheckbox('Smooth?', true);
  smoothCheckbox.position(10, 30);
}


class createPoint { //create a point with its own x and y coordinate, ik could use vectors
  constructor(x_, y_) {
    this.x = 50 + x_;
    this.y = height_ / 2 - y_;
  }
  show() {
    fill(0)
    circle(this.x, this.y, 1)
  }
  writetext() {
    textSize(10)
    text(this.x, this.x, height / 2 + 10)
  }
}

//where all the points will be stored
let points = [];
let xstep = 1;
let ystep = 50;
let looper = 0;

function draw() {
  //set xstep and ystep to their slider values
  ystep = slidery.value();
  xstep = sliderx.value();
  stroke(0);
  background(220);

  //create graph lines
  line(50, height / 2, width - 50, height / 2);
  line(50, 50, 50, height - 50);

  //for every [xstep] pixels calculate y based off equation
  for (i = 0; i < 800; i++) {
    if (smoothCheckbox.checked()) {
      points[i] = new createPoint(i, sin(i / xstep) * ystep); //creates that point as object with x and y value, y = sin(x) 
    } else {
      points[i] = new createPoint(i * xstep, sin(i) * ystep);
    }
  }

  //create line between current and previous point
  for (i = 1; i < 800; i++) {
    stroke(255, 0, 0);
    //create only if point is inside canvas
    if (points[i - 1].y < height) {
      line(points[i - 1].x, points[i - 1].y, points[i].x, points[i].y);
    }
  }

  //create white borders to cut off extreme points
  noStroke(0);
  fill(220);
  rect(width - 50, 0, width, height);
  rect(width, height, width - 50, 0);
  rect(0, 0, width, 50);
  rect(0, height, width, -50);
  looper++;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda