Skip to content

Commit

Permalink
電界ベクトルも仮想関数化
Browse files Browse the repository at this point in the history
  • Loading branch information
CaseyNelson314 committed Jan 16, 2024
1 parent 0c7627a commit d993fab
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 24 deletions.
113 changes: 113 additions & 0 deletions app/script/charge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export abstract class Charge {
/// @brief 指定座標における、この電荷からの電界ベクトルを返す
abstract electricFieldVector: (position: THREE.Vector3) => THREE.Vector3;

/// @brief 電界ベクトルの描画開始座標の配列を返す
abstract electricFieldVectorBeginPositions: () => { vector: THREE.Vector3, opacity: number }[];

/// @brief 任意の座標における電荷との距離^2を返す
abstract distanceSqFrom: (position: THREE.Vector3) => number;

Expand Down Expand Up @@ -102,6 +105,39 @@ export class PointCharge extends Charge {

}

/// @brief 電界ベクトルの描画開始座標の配列を返す
override electricFieldVectorBeginPositions = () => {
const result: { vector: THREE.Vector3, opacity: number }[] = [];

const count = 4;

for (let x = -count; x <= count; x++) {
for (let y = -count; y <= count; y++) {
for (let z = -count; z <= count; z++) {

const length_sq = x ** 2 + y ** 2 + z ** 2;
if (length_sq > count ** 2) {
continue;
}

if (x === 0 && y === 0 && z === 0) {
continue;
}

const opacity = Math.abs(1 - length_sq / (count ** 2));

result.push({
vector: new THREE.Vector3(20 * x, 20 * y, 20 * z),
opacity: opacity
});

}
}
}

return result;
}

/// @brief 任意の座標における電荷との距離を返す
/// @param position 観測点の座標
override distanceSqFrom = (position: THREE.Vector3) => {
Expand Down Expand Up @@ -162,6 +198,15 @@ export class LineCharge extends Charge {

}


/// @brief 電界ベクトルの描画開始座標の配列を返す
override electricFieldVectorBeginPositions = () => {

// TODO
return new Array<{ vector: THREE.Vector3, opacity: number }>();

}

/// @brief 任意の座標における電荷との距離を返す
/// @param position 観測点の座標
override distanceSqFrom = (position: THREE.Vector3) => {
Expand Down Expand Up @@ -264,6 +309,39 @@ export class SphereSurfaceCharge extends Charge {
}

}

/// @brief 電界ベクトルの描画開始座標の配列を返す
override electricFieldVectorBeginPositions = () => {
const result: { vector: THREE.Vector3, opacity: number }[] = [];

const count = 4;

for (let x = -count; x <= count; x++) {
for (let y = -count; y <= count; y++) {
for (let z = -count; z <= count; z++) {

const length_sq = x ** 2 + y ** 2 + z ** 2;
if (length_sq > count ** 2) {
continue;
}

if (x === 0 && y === 0 && z === 0) {
continue;
}

const opacity = Math.abs(1 - length_sq / (count ** 2));

result.push({
vector: new THREE.Vector3(20 * x, 20 * y, 20 * z),
opacity: opacity
});

}
}
}

return result;
}

/// @brief 任意の座標における電荷との距離を返す(外周との距離)
/// @param position 観測点の座標
Expand Down Expand Up @@ -345,6 +423,41 @@ export class SphereVolumeCharge extends Charge

}


/// @brief 電界ベクトルの描画開始座標の配列を返す
override electricFieldVectorBeginPositions = () => {
const result: { vector: THREE.Vector3, opacity: number }[] = [];

const count = 4;

for (let x = -count; x <= count; x++) {
for (let y = -count; y <= count; y++) {
for (let z = -count; z <= count; z++) {

const length_sq = x ** 2 + y ** 2 + z ** 2;
if (length_sq > count ** 2) {
continue;
}

if (x === 0 && y === 0 && z === 0) {
continue;
}

const opacity = Math.abs(1 - length_sq / (count ** 2));

result.push({
vector: new THREE.Vector3(20 * x, 20 * y, 20 * z),
opacity: opacity
});

}
}
}

return result;
}


/// @brief 任意の座標における電荷との距離を返す(外周との距離)
/// @param position 観測点の座標
override distanceSqFrom = (position: THREE.Vector3) => {
Expand Down
25 changes: 5 additions & 20 deletions app/script/field3d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,31 +177,16 @@ class ElectricFieldVectors3D extends THREE.Object3D {

}

const count = 4;
for (let charge of this.charges) {
for (const charge of this.charges) {

if (charge.getChargeType() === ChargeType.Neutral) {
continue;
}

for (let x = -count; x <= count; x++) {
for (let y = -count; y <= count; y++) {
for (let z = -count; z <= count; z++) {

const length_sq = x ** 2 + y ** 2 + z ** 2;
if (length_sq > count ** 2) {
continue;
}

if (x === 0 && y === 0 && z === 0) {
continue;
}

const opacity = Math.abs(1 - length_sq / (count ** 2));
AddArrow(new THREE.Vector3(x * 20, y * 20, z * 20).add(charge.position), opacity);

}
}
const points = charge.electricFieldVectorBeginPositions();
for (const point of points)
{
AddArrow(point.vector.add(charge.position), point.opacity);
}

}
Expand Down
6 changes: 3 additions & 3 deletions app/script/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ const start = () => {
lineDensity.value = lineCharge.lineDensity.toFixed(3);

// 線電荷は長さを変更できる
const lineLength = document.getElementById("charge_length") as HTMLInputElement;
lineLength.labels![0]!.style.display = "block";
lineLength.value = lineCharge.length.toFixed(3);
// const lineLength = document.getElementById("charge_length") as HTMLInputElement;
// lineLength.labels![0]!.style.display = "block";
// lineLength.value = lineCharge.length.toFixed(3);
}
else if (charge instanceof SphereSurfaceCharge) {
const sphereSurfaceCharge = charge as SphereSurfaceCharge;
Expand Down
2 changes: 1 addition & 1 deletion app/style/control.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ input[type="number"] {
width: 80px;
text-align: right;
padding-right: 10px;
font-size: 20px;
font-size: 17px;
color: #e3e3e3;
background-color: #38393b;
border: 2px solid #4f4f4f;
Expand Down

0 comments on commit d993fab

Please sign in to comment.