prime-map-generator/src/main/js/Model.ts

47 lines
1.2 KiB
TypeScript

import {CachedPrimeMath, PrimeMath} from "./PrimeMath";
/**
* Represents a particular way in which prime numbers can be arranged and drawn.
*/
export interface Model {
/**
* Returns `true` if and only if the square at coordinates (`x`, `y`) is a prime number.
*
* @param x the horizontal coordinate
* @param y the vertical coordinate
* @return `true` if and only if the square at coordinates (`x`, `y`) is a prime number
*/
isPrime(x: number, y: number): boolean | null;
}
/**
* A `Model` for a simple grid of primes.
*
* Supports scrolling by setting the `scrollX` and `scrollY` values.
*/
export class GridModel implements Model {
private readonly primeMath: PrimeMath = CachedPrimeMath.getInstance();
/**
* The vertical position of the viewport.
*/
public scrollX: number = 0;
/**
* The horizontal position of the viewport.
*/
public scrollY: number = 0;
/**
* The number of columns to draw primes in.
*/
public cols: number = 100;
isPrime(x: number, y: number): boolean | null {
if (this.scrollX + x >= this.cols) return null;
const n = (y + this.scrollY) * this.cols + this.scrollX + x + 1;
return this.primeMath.isPrime(n);
}
}