limiting siPrefix group + moved voltageStr,currentStr into Circuit

This commit is contained in:
Patrick Haßel 2025-02-04 11:17:01 +01:00
parent ab084d867a
commit c09245d7b1
5 changed files with 39 additions and 23 deletions

View File

@ -6,9 +6,12 @@ import {Light} from '../parts/light/Light';
import {Relay} from '../parts/relay/Relay'; import {Relay} from '../parts/relay/Relay';
import {Junction} from '../junction/Junction'; import {Junction} from '../junction/Junction';
import {Switch} from '../parts/switch/Switch'; import {Switch} from '../parts/switch/Switch';
import {siPrefix} from '../siPrefix';
export class Circuit { export class Circuit {
private siPrefixRange: number = 2;
constructor( constructor(
readonly uuid: string, readonly uuid: string,
public name: string, public name: string,
@ -97,4 +100,12 @@ export class Circuit {
this.calculate(); this.calculate();
} }
voltageStr(voltage: number) {
return siPrefix(Math.abs(voltage), 'V', 2, -this.siPrefixRange, +this.siPrefixRange);
}
currentStr(current: number) {
return siPrefix(Math.abs(current) || 0, 'A', 2, -this.siPrefixRange, +this.siPrefixRange);
}
} }

View File

@ -1,7 +1,6 @@
import {Part} from "../Part"; import {Part} from "../Part";
import {Junction} from '../../junction/Junction'; import {Junction} from '../../junction/Junction';
import {PartType} from '../PartType'; import {PartType} from '../PartType';
import {siPrefix} from '../../siPrefix';
import {Wire} from '../../wire/Wire'; import {Wire} from '../../wire/Wire';
import {Circuit} from '../../circuit/Circuit'; import {Circuit} from '../../circuit/Circuit';
@ -37,11 +36,11 @@ export class Battery extends Part {
} }
get voltageStr(): string { get voltageStr(): string {
return siPrefix(Math.abs(this.voltage), 'V', 2); return this.circuit.voltageStr(this.voltage);
} }
get currentStr(): string { get currentStr(): string {
return siPrefix(Math.abs(this.current) || 0, 'A', 2); return this.circuit.currentStr(this.current);
} }
} }

View File

@ -1,7 +1,6 @@
import {Part} from "../Part"; import {Part} from "../Part";
import {Junction} from '../../junction/Junction'; import {Junction} from '../../junction/Junction';
import {PartType} from '../PartType'; import {PartType} from '../PartType';
import {siPrefix} from '../../siPrefix';
import {fadeColor} from '../../colorHelpers'; import {fadeColor} from '../../colorHelpers';
import {Wire} from '../../wire/Wire'; import {Wire} from '../../wire/Wire';
@ -40,10 +39,6 @@ export class Light extends Part {
return this.voltage > this.voltageMax; return this.voltage > this.voltageMax;
} }
get voltageStr(): string {
return siPrefix(Math.abs(this.voltage), 'V', 2);
}
get current(): number { get current(): number {
if (this.a.voltage === null || this.b.voltage === null) { if (this.a.voltage === null || this.b.voltage === null) {
return 0; return 0;
@ -51,8 +46,12 @@ export class Light extends Part {
return (this.b.voltage - this.a.voltage) / this.resistance; return (this.b.voltage - this.a.voltage) / this.resistance;
} }
get voltageStr(): string {
return this.circuit.voltageStr(this.voltage);
}
get currentStr(): string { get currentStr(): string {
return siPrefix(Math.abs(this.current) || 0, 'A', 2); return this.circuit.currentStr(this.current);
} }
fill(): string { fill(): string {

View File

@ -1,7 +1,6 @@
import {Part} from "../Part"; import {Part} from "../Part";
import {Junction} from '../../junction/Junction'; import {Junction} from '../../junction/Junction';
import {PartType} from '../PartType'; import {PartType} from '../PartType';
import {siPrefix} from '../../siPrefix';
import {Wire} from '../../wire/Wire'; import {Wire} from '../../wire/Wire';
import {RESISTANCE_MIN} from '../../circuit/Calculation'; import {RESISTANCE_MIN} from '../../circuit/Calculation';
import {Circuit} from '../../circuit/Circuit'; import {Circuit} from '../../circuit/Circuit';
@ -54,10 +53,6 @@ export class Relay extends Part {
return this.voltage > this.voltageMax; return this.voltage > this.voltageMax;
} }
get voltageStr(): string {
return siPrefix(Math.abs(this.voltage), 'V', 2);
}
get current(): number { get current(): number {
if (this.coilA.voltage === null || this.coilB.voltage === null) { if (this.coilA.voltage === null || this.coilB.voltage === null) {
return 0; return 0;
@ -65,8 +60,12 @@ export class Relay extends Part {
return (this.coilB.voltage - this.coilA.voltage) / this.resistance; return (this.coilB.voltage - this.coilA.voltage) / this.resistance;
} }
get voltageStr(): string {
return this.circuit.voltageStr(this.voltage);
}
get currentStr(): string { get currentStr(): string {
return siPrefix(Math.abs(this.current) || 0, 'A', 2); return this.circuit.currentStr(this.current);
} }
override postCalculate(): boolean { override postCalculate(): boolean {

View File

@ -1,18 +1,26 @@
export const SI_PREFIXES: string[] = ['q', 'r', 'y', 'z', 'a', 'f', 'p', 'n', 'µ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q']; export const SI_PREFIXES: string[] = ['q', 'r', 'y', 'z', 'a', 'f', 'p', 'n', 'µ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q'];
export function siPrefix(value: number, unit: string, minDigits: number): string { export function siPrefix(value: number, unit: string, minDigits: number, minGroup: number = -10, maxGroup: number = +10): string {
if (value === 0) { if (value === 0) {
return `0${unit}`; return `0${unit}`;
} }
minGroup = Math.max(-10, minGroup);
maxGroup = Math.min(+10, maxGroup);
const negative = value < 0; const negative = value < 0;
value = negative ? -value : value; value = negative ? -value : value;
const exp0 = Math.log10(value);
const group = Math.max(-10, Math.min(+10, Math.floor(exp0 / 3))); const measureExponent = Math.log10(value);
const index = group + 10; const prefixGroup = Math.max(minGroup, Math.min(maxGroup, Math.floor(measureExponent / 3)));
const prefix = SI_PREFIXES[index]; const prefixIndex = prefixGroup + 10;
const exp1 = group * 3; const prefix = SI_PREFIXES[prefixIndex];
const factor = Math.pow(10, -exp1); const scalerExponent = prefixGroup * 3;
const newValue = factor * value; if (value < Math.pow(10, scalerExponent)) {
return `0${unit}`;
}
const factor = Math.pow(10, -scalerExponent);
const newValue = value * factor;
const hasDigits = Math.floor(Math.log10(newValue)) + 1; const hasDigits = Math.floor(Math.log10(newValue)) + 1;
const decimals = Math.max(0, minDigits - hasDigits); const decimals = Math.max(0, minDigits - hasDigits);
const newValueStr2 = newValue.toFixed(decimals); const newValueStr2 = newValue.toFixed(decimals);