Advertisement
Guest User

Untitled

a guest
Nov 6th, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. //properties of displays
  2. const display = {
  3. W: 1200,
  4. H: 500
  5. };
  6.  
  7. const simDisplay = {
  8. borderLeft: 0,
  9. borderUp: 0,
  10. W: 800,
  11. H: display.H
  12. };
  13.  
  14. let simCanvas;
  15.  
  16. let magneticDipoles = [];
  17.  
  18. let abstractDipole = {x: 300, y: 0, px: simDisplay.W / 2, py: simDisplay.H / 2}
  19. let mu = {x: 1, y: 1}
  20.  
  21.  
  22. function setup() {
  23. outerCanvas = createCanvas(display.W, display.H);
  24. outerCanvas.parent('simwrapper');
  25.  
  26. simCanvas = createGraphics(simDisplay.W, simDisplay.W)
  27.  
  28. backgroundColor = color(20);
  29.  
  30. setMagneticDipoles(abstractDipole);
  31. }
  32.  
  33. function draw() {
  34. background(backgroundColor);
  35.  
  36. simCanvas.clear();
  37. drawMagneticVectors();
  38. drawAbstractMagnet();
  39. image(simCanvas, 0, 0);
  40. }
  41.  
  42. const vectorSpacing = 30;
  43. const vectorPadding = 20;
  44.  
  45. const scaling = 200
  46. const fMax = 0.0002;
  47.  
  48. function drawMagneticVectors() {
  49.  
  50. let magneticVectorMin = Bvector({px: simDisplay.W, py: 0});
  51. let fMin = Math.sqrt(magneticVectorMin.x*magneticVectorMin.x + magneticVectorMin.y*magneticVectorMin.y);
  52.  
  53. let saturationOffset = 255 * fMin / (fMax + fMin);
  54. let saturationScalar = (255 - saturationOffset) / fMax;
  55.  
  56. simCanvas.noStroke();
  57. for (let x = vectorPadding; x <= simDisplay.W - vectorPadding; x += vectorSpacing) {
  58. for (let y = vectorPadding; y <= simDisplay.H - vectorPadding; y += vectorSpacing) {
  59. let magneticVector = Bvector({px: x, py: y});
  60.  
  61. let fieldStrength = Math.sqrt(magneticVector.x*magneticVector.x + magneticVector.y*magneticVector.y);
  62.  
  63. let colorSaturation = Math.min(Math.max(fieldStrength * saturationScalar + saturationOffset, 0), 255);
  64.  
  65. let angle = Math.atan2(magneticVector.y, magneticVector.x);
  66. let cosa = Math.cos(angle);
  67. let sina = Math.sin(angle);
  68.  
  69. // Rotate points by angle
  70. p1 = createVector(-sina * 3, cosa * 3);
  71. p2 = createVector(-sina * -3, cosa * -3);
  72. p3 = createVector(cosa * 10, sina * 10);
  73.  
  74. simCanvas.fill(255, 255, 255, colorSaturation);
  75. simCanvas.triangle(x + p1.x, y + p1.y, x + p2.x, y + p2.y, x + p3.x, y + p3.y);
  76. simCanvas.fill(255, 0, 0, colorSaturation);
  77. simCanvas.triangle(x - p1.x, y - p1.y, x - p2.x, y - p2.y, x - p3.x, y - p3.y);
  78. }
  79. }
  80. }
  81.  
  82. const abstractMagnetWidth = 50
  83.  
  84. function drawAbstractMagnet() {
  85. let adipole = abstractDipole;
  86. let length = sqrt(adipole.x*adipole.x + adipole.y*adipole.y);
  87. let step = {x: adipole.x / length, y: adipole.y / length};
  88. let pStep = {x: step.y, y: -step.x}
  89.  
  90. let x1 = (step.x * length + pStep.x * abstractMagnetWidth) / 2
  91. let x2 = (step.x * length - pStep.x * abstractMagnetWidth) / 2
  92. let y1 = (step.y * length + pStep.y * abstractMagnetWidth) / 2
  93. let y2 = (step.y * length - pStep.y * abstractMagnetWidth) / 2
  94.  
  95. simCanvas.fill('grey')
  96.  
  97. simCanvas.stroke(255)
  98. simCanvas.beginShape()
  99. simCanvas.vertex(x1 + adipole.px, y1 + adipole.py)
  100. simCanvas.vertex(x2 + adipole.px, y2 + adipole.py)
  101. simCanvas.vertex(-x1 + adipole.px, -y1 + adipole.py)
  102. simCanvas.vertex(-x2 + adipole.px, -y2 + adipole.py)
  103. simCanvas.endShape(CLOSE)
  104. }
  105.  
  106. function Bvector(r) {
  107. let B = {x: 0, y:0};
  108.  
  109. magneticDipoles.forEach(md => {
  110. let nr = {px: r.px - md.px, py: r.py - md.py};
  111. let rl = Math.sqrt(nr.px*nr.px + nr.py*nr.py)
  112.  
  113. let r3 = Math.pow(rl, 3);
  114. let mudotr = md.x * nr.px / rl + md.y * nr.py / rl;
  115. B.x += (3 * mudotr * nr.px / rl - md.x) / (r3 * magneticDipoles.length);
  116. B.y += (3 * mudotr * nr.py / rl - md.y) / (r3 * magneticDipoles.length);
  117. });
  118.  
  119.  
  120. return B;
  121. }
  122.  
  123. const dipoleDistance = 10;
  124.  
  125. function setMagneticDipoles(adipole) {
  126. magneticDipoles = [];
  127. let length = sqrt(adipole.x*adipole.x + adipole.y*adipole.y);
  128. let step = {x: adipole.x / length, y: adipole.y / length};
  129. let startPos = {px: adipole.px - step.x * length / 2, py: adipole.py - step.y * length / 2};
  130.  
  131. magneticDipoles.push({x: step.x, y: step.y, px: startPos.px, py: startPos.py})
  132. magneticDipoles.push({x: step.x, y: step.y, px: startPos.px + step.x * length, py: startPos.py + step.y * length})
  133.  
  134. for (let k = 0; k <= length; k += dipoleDistance) {
  135. magneticDipoles.push({x: step.x * 1000, y: step.y * 1000, px: startPos.px + step.x * k, py: startPos.py + step.y * k})
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement