
Knowledgable n00b
[RESOLVED] Extending a vector by a number of pixels
Hello, Sandy here.
I'm trying to extend a line from any given point to the mouse by a certain number of pixels (say, 100).
This is the closest I've gotten:
link to SWF
As you can see, the red line is behaving as it should (connecting the "any" point, located at the center of the screen, with the mouse) but the blue line is extending a line from the origin to the mouse by 100 rather than from the "any" point.
I've made a little diagram to help illustrate my most recent attempt:
and the AS3 code on the first frame (shown above, complete below):
Actionscript Code:
import flash.events.Event; import flash.geom.Point;
function findTermination(x0:Number, y0:Number, x1:Number, y1:Number):Point { var p:Point = new Point(x1  x0, y1  y0); var d:Number = mag(p); var l:Number = d + 100; var t:Point = new Point(p.x * l / d, p.y * l / d); var r:Point = new Point(t.x + x0, t.y + y0); return r; } function mag(v:Point):Number { return Math.sqrt(v.x * v.x + v.y * v.y); }
this.addEventListener(Event.ENTER_FRAME, fr);
function fr(e:Event) { this.graphics.clear(); var end:Point = findTermination(this.width / 2, this.height / 2, mouseX, mouseY); this.graphics.lineStyle(1, 0xff0000); this.graphics.moveTo(512, 384); this.graphics.lineTo(mouseX, mouseY); this.graphics.lineStyle(1, 0x0000ff); this.graphics.lineTo(end.x, end.y); }
I've expanded the findTermination function as much as I can to make it easier to read (I have a real bad habit of doing everything on as few lines as possible).
If anyone has any insight I'd be super thrilled. Thanks, guys!
S
Last edited by BlueGeek; 03162011 at 01:03 PM.
"...there were only two good books in the hotel gift shop and I had written both of them."
Douglas Adams

alright i finally got on the forum. what does graphics.moveTo() do? it looks like an origin shift

in case fb deleted my chat message, try putting (512, 384) or (this.graphics.width/2, this.graphics.height/2) into findTermination

Knowledgable n00b
AHHH, I'm an idiot. I'm still using AS2 stage dimension variables. The correct syntax is stage.stageHeight and stage.stageWidth.
"...there were only two good books in the hotel gift shop and I had written both of them."
Douglas Adams

Knowledgable n00b
the corrected code:
Actionscript Code:
import flash.events.Event; import flash.geom.Point;
function findTermination(x0:Number, y0:Number, x1:Number, y1:Number):Point { var p:Point = new Point(x1  x0, y1  y0); var d:Number = mag(p); var l:Number = d + 100; var t:Point = new Point(p.x * l / d, p.y * l / d); var r:Point = new Point(t.x + x0, t.y + y0); return r; } function mag(v:Point):Number { return Math.sqrt(v.x * v.x + v.y * v.y); }
this.addEventListener(Event.ENTER_FRAME, fr);
function fr(e:Event) { this.graphics.clear(); var end:Point = findTermination(stage.stageWidth / 2, stage.stageHeight / 2, mouseX, mouseY); this.graphics.lineStyle(1, 0xff0000); this.graphics.moveTo(stage.stageWidth / 2, stage.stageHeight / 2); this.graphics.lineTo(mouseX, mouseY); this.graphics.lineStyle(1, 0x0000ff); this.graphics.lineTo(end.x, end.y); }
"...there were only two good books in the hotel gift shop and I had written both of them."
Douglas Adams
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
