# Help -- shrinking the distance between x,y coordinates

**URL:** https://forum.kirupa.com/t/help-shrinking-the-distance-between-x-y-coordinates/129869
**Category:** Uncategorized
**Created:** [November 29, 2004, 4:40pm UTC](https://forum.kirupa.com/t/help-shrinking-the-distance-between-x-y-coordinates/129869 "2004-11-29T16:40:14Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![wuuf](https://avatars.discourse-cdn.com/v4/letter/w/5e9695/32.png) [@wuuf](https://forum.kirupa.com/u/wuuf)
#### Post date: [November 29, 2004, 4:40pm UTC](https://forum.kirupa.com/t/help-shrinking-the-distance-between-x-y-coordinates/129869/1 "2004-11-29T16:40:14Z")

</div>

hey…

i am trying to create code that will shrink the distance between x,y coordinates by 50%

it works but only when x,y coordinates move from north to south, for example…  
0,0  
0,10  
0,20  
0,30  
0,40  
0,50

becomes (which is right)  
0,0  
0,5  
0,10  
0,15  
0,20  
0,25

however when the x,y coordinates move south to north…  
0,0  
0,-10  
0,-20  
0,-30  
0,-40  
0,-50

it remains (and is wrong)  
0,0  
0,5  
0,10  
0,15  
0,20  
0,25

instead of (which would be right)  
0,-0  
0,-5  
0,-10  
0,-15  
0,-20  
0,-25

the problem i believe lies with this part of the function…

ang = (ang - 90);  
a = degToRad(ang);  
xxx = xx + Math.cos(a) \* radius;  
yyy = yy - Math.sin(a) \* radius;

i’m nearly there, i’m just doing something fundamentally wrong with the math calculating the x,y coordinates

if anybody has any ideas, i could really use the help, i got to be missing something pretty simple…

the code is in the function testShrink, below it are the support functions

if you want to test it in flash, i’ve created a function that creates a test list of x,y coordinates, there is also a function that will automatically give you a graphical representation of the points; white line (original), red line (new, shrunk)

thanks  
…

//creates a test list of x,y coordinates 0,0 0,10 0,20 0,30 0,40 0,50 0,60 0,70 0,80  
//“down” creates a list from 0,0 to 0,80  
//“up” creates a list from 0,0 to 0,-80  
create\_testList(“down”);

//fires function using testList as x,y coordinates to shrink  
testShrink(testList);

//the function i’ve created consists of three components…  
//a) determine distance between points  
//b) determine angle between points  
//c) use distance and angle to create new series of points

function testShrink (arr):Void {  
var distList:Array = new Array();  
var angList:Array = new Array();  
var shrinkList:Array = new Array();

//puts orgin x,y coordinates in shrinkList array  
this[“temp” + 0] = new Array();  
this[“temp” + 0][0] = arr[0][0];  
this[“temp” + 0][1] = arr[0][1];  
shrinkList[0] = this[“temp” + 0];

//determines distance between points and puts in distList array  
for (var i=0; i\<arr.length; i++) {  
var n = hypot(arr\*[0],arr\*[1],arr[i+1][0],arr[i+1][1]);  
n = Math.round(n/2);  
distList.push(n);  
trace(“distance…”+i+"  
"+n);  
}

//determines angle between points and puts in angList array  
for (var k=2; k\<arr.length; k++) {  
var n = findDegree(arr,k);  
angList.push(n);  
trace(“degree…”+k+"  
"+n);  
}

//creates new x,y coordinates and puts in shrinkList array  
for (var i=0; i\<arr.length-2; i++) {

var a,xxx,yyy;  
var ang = angList\*;  
var radius = distList\*;  
var xx = shrinkList\*[0];  
var yy = shrinkList\*[1];

ang = (ang - 90);  
a = degToRad(ang);

xxx = xx + Math.cos(a) \* radius;  
yyy = yy - Math.sin(a) \* radius;

this[“temp” + i] = new Array();  
this[“temp” + i][0] = Math.round(xxx);  
this[“temp” + i][1] = Math.round(yyy);

shrinkList[i+1] = this[“temp” + i];  
}

trace(“original…”+arr.length+"  
“+arr.join(”  
“));  
trace(“shrinkList…”+shrinkList.length+”  
“+shrinkList.join(”  
"));

//create graphical representation of old and new points  
drawXY(arr,shrinkList);  
}

/////////////////////////////////////  
//SUPPORT FUNCTIONS

function create\_testList (xxx:String) {  
var upDown = 1;  
if (xxx==“up”) {upDown=-1;};

testList = new Array();  
for (var i=0; i\<9; i++) {  
this[“temp” + i] = new Array();  
this[“temp” + i][0] = 0;  
this[“temp” + i][1] = (10 \* i) \* upDown;  
testList\* = this[“temp” + i];  
}  
//trace(testList.join("  
"));  
}

function hypot (x,y,xx,yy) {  
var n = Math.pow( (diff(x,xx)),2 ) + Math.pow( (diff(y,yy)),2 );  
n = Math.round(Math.sqrt(n));  
return n;  
}

function findDegree (list,n) {  
var xxx,yyy,deltaX,deltaY,rad,ang,degree,a;  
var x = list[n-1][0];  
var y = list[n-1][1];  
var xx = list[n][0];  
var yy = list[n][1];

if (n==0) {  
x = list[0][0];  
y = list[0][1];  
}

deltaX = xx-x;  
deltaY = yy-y;  
rad = Math.atan2(deltaY,deltaX);  
ang = (rad/Math.PI)\*180;

if (yy\<y) {  
degree = (Math.abs(ang) - 90);  
} else {  
degree = (Math.abs(ang) - 90) \* -1;  
}

return roundToDecimal(degree,2);  
}

function degToRad (d) {  
return (d/180)\*Math.PI;  
}

function roundToDecimal (theNumber, decPlaces) {  
if (decPlaces \>= 0) {  
var temp = Math.pow(10, decPlaces);  
return Math.round(theNumber \* temp)/temp;  
}  
}

function diff (r,s) {  
return Math.abs(r-s);  
}

function drawXY (listA,listB) {  
//listA  
\_root.createEmptyMovieClip(“path0”,0);  
\_root.path0.lineStyle(10,0x000000,100);  
\_root.path0.moveTo(listA[0][0],listA[0][1]);

for (var j=1; j\<listA.length; j++) {  
\_root.path0.lineTo(listA[j][0],listA[j][1]);  
}

//listB  
\_root.createEmptyMovieClip(“path1”,1);  
\_root.path1.lineStyle(2,0xFF0000,100);  
\_root.path1.moveTo(listB[0][0],listB[0][1]);

for (var j=1; j\<listB.length; j++) {  
\_root.path1.lineTo(listB[j][0],listB[j][1]);  
}  
}  
//////////////////////////////////////////
