TheAlgorithms/JavaScript · error · Error

imageWidth should be greater than zero

Error message

imageWidth should be greater than zero

What it means

The getRGBData function renders the Mandelbrot set as RGB pixel data. imageWidth must be positive because it determines the number of horizontal pixels in the output. Zero or negative width produces an empty or invalid image buffer.

Source

Thrown at Maths/Mandelbrot.js:40

 * @param {number} imageHeight The height of the rendered image.
 * @param {number} figureCenterX The x-coordinate of the center of the figure.
 * @param {number} figureCenterY The y-coordinate of the center of the figure.
 * @param {number} figureWidth The width of the figure.
 * @param {number} maxStep Maximum number of steps to check for divergent behavior.
 * @param {boolean} useDistanceColorCoding Render in color or black and white.
 * @return {object} The RGB-data of the rendered Mandelbrot set.
 */
export function getRGBData(
  imageWidth = 800,
  imageHeight = 600,
  figureCenterX = -0.6,
  figureCenterY = 0,
  figureWidth = 3.2,
  maxStep = 50,
  useDistanceColorCoding = true
) {
  if (imageWidth <= 0) {
    throw new Error('imageWidth should be greater than zero')
  }

  if (imageHeight <= 0) {
    throw new Error('imageHeight should be greater than zero')
  }

  if (maxStep <= 0) {
    throw new Error('maxStep should be greater than zero')
  }

  const rgbData = []
  const figureHeight = (figureWidth / imageWidth) * imageHeight

  // loop through the image-coordinates
  for (let imageX = 0; imageX < imageWidth; imageX++) {
    rgbData[imageX] = []
    for (let imageY = 0; imageY < imageHeight; imageY++) {
      // determine the figure-coordinates based on the image-coordinates

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Pass a positive integer for imageWidth.
  2. Validate dimensions are > 0 before calling getRGBData.
  3. Provide a fallback default when config values for image size are missing or invalid.

Example fix

// before
getRGBData(parsedWidth, parsedHeight)
// after
if (parsedWidth <= 0 || parsedHeight <= 0) throw new RangeError('dimensions must be positive')
getRGBData(parsedWidth, parsedHeight)
Defensive patterns

Strategy: validation

Validate before calling

if (typeof imageWidth !== 'number' || imageWidth <= 0) {
  throw new RangeError('imageWidth must be a positive number')
}
getRGBData(imageWidth, imageHeight, figureCenterX, figureCenterY, figureWidth, maxStep, useDistanceColorCoding)

Type guard

const isPositiveDimension = (d) => typeof d === 'number' && d > 0

Prevention

When it happens

Trigger: Calling getRGBData(0, 600) or getRGBData(-800, 600). Any imageWidth <= 0 triggers this error before imageHeight or maxStep are checked.

Common situations: Zero from a division result, negative from a subtraction, reading dimensions from a config that was not set, or a default value overridden by invalid user input.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/aeabaa132a7e7749. Report an issue: GitHub.