[ascoders] AW: punkt an linerarer funktion (gerade) spiegeln

morning boys & girls!
 
ich beantworte mal schnell meine eigene frage ... und noch bisschen mehr.
quite nützlich! ;)
 
 
cheers,
eric.
 
 
var $line = Math.getLinePP({x:20, y:0}, {x:100, y:100});
var $point = {x:30, y: 45};
 
var $symmetryPoint = Math.getSymmetryPoint($line, $point);
 
this.createEmptyMovieClip('a_mc', 100);
this.a_mc.lineStyle(1, 0x00FF00, 100);
this.a_mc.drawLineLXX($line, 0, 100);
 
this.createEmptyMovieClip('b_mc', 101);
this.b_mc.lineStyle(1, 0xFF0000, 100);
this.b_mc.drawDot($point);
 
this.createEmptyMovieClip('c_mc', 102);
this.c_mc.lineStyle(1, 0x0000FF, 100);
this.c_mc.drawDot($symmetryPoint);
 
____________________________________________________________________________
____
 
MovieClip.prototype.drawLinePP = function(p1, p2) {
 this.moveTo(p1.x - 2, p1.y - 2);
 this.lineTo(p1.x + 2, p1.y + 2);
 this.moveTo(p1.x - 2, p1.y + 2);
 this.lineTo(p1.x + 2, p1.y - 2);
}
 
MovieClip.prototype.drawLineLXX = function(l, x1, x2, y1, y2) {
 if (y1 == null && y2 == null) {
  var p1 = new Object();
  p1.x = x1;
  p1.y = l.a * x1 + l.b;
  
  var p2 = new Object();
  p2.x = x2;
  p2.y = l.a * x2 + l.b;
  
  if (Math.abs(l.a) > 25) { // drawing-fix!
   p1.x = p2.x = l.d;
   p1.y = +1000;
   p2.y = -1000;
  }
  
  this.drawLinePP(p1, p2);
 } else if (x1 == null && x2 == null){
  var p1 = new Object();
  p1.y = y1;
  p1.x = (y1 - l.b) / l.a;
  
  var p2 = new Object();
  p2.y = y2;
  p2.x = (y2 - l.b) / l.a;
  
  // no drawing-fix!
  
  this.drawLinePP(p1, p2);
 } else {
  return null;
 }
}
 
MovieClip.prototype.drawDot = function(p1) {
 this.moveTo(p1.x - 1, p1.y - 1);
 this.lineTo(p1.x + 1, p1.y + 1);
}
 
Math.getLinePP = function(p1, p2) {
 var l = new Object();
 
 var x1 = p1.x;
 var y1 = p1.y;
 var x2 = p2.x;
 var y2 = p2.y;
 
 if (x1 == x2) {
  if (y1 == y2) {
   l = null;
  } else {
   l.c = x1;
  }
 } else {
  l.a = (y2 - y1) / (x2 - x1);
  l.b = (y1 - (l.a * x1));
 }
 return (l);
}
 
Math.getLinePV = function(p0, v0) {
 var l = new Object();
 
 l.d = p0.x; // d: drawing-fix!
 if (v0.x == 0) {
  l.c = p0.x;
 } else {
  l.a = v0.y / v0.x;
  l.b = p0.y - (l.a * p0.x);
 }
 return (l);
}
 
Math.getNormalLinePV = function(p0, l0) {
 //if (p0.y != l0.a * p0.x + l0.b) return null;
 
 var v0N = new Object();
 
 v0N.x = l0.a * (-1);
 v0N.y = 1;
 
 return(this.getLinePV(p0, v0N));
}
 
Math.getCenter = function(p1, p2) {
 return {x: ((p2.x - p1.x) / 2 + p1.x), y: ((p2.y - p1.y) / 2 + p1.y)};
}
 
Math.getSymmetryPoint = function(l, p) {
 var $n = Math.getNormalLinePV(p, l);
 var $i = Math.getLineIntersection(l, $n);
 
 var xd = ($i.x - p.x) * 2;
 var yd = ($i.y - p.y) * 2;
 
 var x = p.x + xd;
 var y = p.y + yd;
 
 return {x: x, y: y};
}
 
Math.getLineIntersection = function(l0, l1) {
 if (l0 == null || l1 == null) return null;
 
 var a0 = l0.a;
 var b0 = l0.b;
 var c0 = l0.c;
 var a1 = l1.a;
 var b1 = l1.b;
 var c1 = l1.c;
 var u;
 
 if (c0 == undefined && c1 == undefined) {
  
  if (a0 == a1) return null; // lines parallel
 
  u = (b1 - b0) / (a0 - a1);  
 
  return {x: u, y: (a0*u + b0)};
 } else if (c0 != undefined) {
  
  if (c1 != undefined) {
   return null; // both lines vertical
  } else {
   return {x: c0, y: (a1*c0 + b1)};
  }
  
 } else if (c1 != undefined) {
  return {x: c1, y: (a0*c1 + b0)};
 }
}

Other related posts: