Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
stack0s

how do I x y intercept?

Recommended Posts

Posted · Original PosterOP

Hi I am trying to use some Actionscript 2.0 for this problem but if you can abstraction to basic algebra I think my intentions are clear...

 

I am simply trying to calculate some X Y intercepts but am getting poor results... I studied the algebraic formula to derive this program

 

However! I am still getting the wrong answers. Can someone please help me fix this?

 

 

Here is my code

test = true;
var x1 = 3;
var y1 = -12;
var x2 = 16;
var y2 = -10;
var x3 = 8;
var y3 = -15;
var x4 = 12;
var y4 = -1;

function determineXY():Void {
	//Solve
	//initiate lines
	//line1
	//m
	var line1m = ((y2-y1)/(x2-x1));
	var line1msave = line1m;
	//b
	var line1b = (y1-(line1m*x1));
	var line1bsave = line1b;
	//line2
	//m
	var line2m = ((y4-y3)/(x4-x3));
	var line2msave = line2m;
	//b
	var line2b = (y3-(line2m*x3));
	var line2bsave = line2b;
	//Solve X
	//determine m(x)
	if (line1b>=0) {
		line2m = Math.abs(line2m)*1;
	}
	if (line1b<0) {
		line2m = Math.abs(line2m)*-1;
	}
	//determine b   
	if (line2msave>=0) {
		line1b = Math.abs(line1b)*1;
	}
	if (line2msave<0) {
		line1b = Math.abs(line1b)*-1;
	}
	xint = ((line1b+line2b)/(line1m+line2m));
	//Solve Y
	yint = ((line2msave*xint)+line2bsave);
}
function getXYIntercept(inputX1:Number, inputY1:Number, inputX2:Number, inputY2:Number, inputX3:Number, inputY3:Number, inputX4:Number, inputY4:Number):Void {
	x1 = inputX1;
	y1 = inputY1;
	x2 = inputX2;
	y2 = inputY2;
	x3 = inputX3;
	y3 = inputY3;
	x4 = inputX4;
	y4 = inputY4;
	determineXY();
    
	//draw (this is just to visual the results)
	_root.createEmptyMovieClip("holder", 1);
	holder.lineStyle(2, 0x000000, 100);
	holder.moveTo(x1, y1);
	holder.lineTo(x2, y3);
	holder.moveTo(x3, y3);
	holder.lineTo(x4, y4);
	intersection._x = xint;
	intersection._y = yint;
	
}
onEnterFrame = function () {
	if (test) {
		getXYIntercept(x1,y1,x2,y2,x3,y3,x4,y4);
		trace(xint);
		trace(yint)
	}
	test = false;
};

This is it... only thing not in the code is the movie clip referenced above

Link to post
Share on other sites
2 hours ago, stack0s said:

Hi I am trying to use some Actionscript 2.0 for this problem but if you can abstraction to basic algebra I think my intentions are clear...

 

I am simply trying to calculate some X Y intercepts but am getting poor results... I studied the algebraic formula to derive this program

 

However! I am still getting the wrong answers. Can someone please help me fix this?

 

Here is my code

 

<snip>

 

This is it... only thing not in the code is the movie clip referenced above

Take a look at this, compiles and runs, c++

/* The Ocular Man */

#include <iostream>

double findM(double x1, double y1, double x2, double y2);
double findB(double y, double x, double m);
double findXintercept(double m, double b);
double findYintercept(double m, double b);

int main()
{
    std::cout << "Goodbye world!" << std::endl;

    //x-intercept, cross x axis (n,0)
    //y-intercept, cross y axis (0,n)

    //set 1
    int x1 = 3;
    int y1 = -12;
    int x2 = 16;
    int y2 = -10;
    
    //set 2
    /* unused
    int x3 = 8;
    int y3 = -15;

    int x4 = 12;
    int y4 = -1;
    */

    double m = findM(x1, y1, x2, y2);
    std::cout << "m: " << m << std::endl;

    double b = findB(y1, x1, m);
    std::cout << "b: " << b << std::endl;

    double Xi = findXintercept(m, b);
    std::cout << "X intercept: " << Xi << std::endl;

    double Yi = findYintercept(m, b);
    std::cout << "Y intercept: " << Yi << std::endl;

    return 0;
}

//find slope
double findM(double x1, double y1, double x2, double y2) {
    return (y2-y1)/(x2-x1);
}

//find b
double findB(double y, double x, double m) {
    return (y/(m*x));
}

//find X intercept
double findXintercept(double m, double b) {
    return (0-b)/m;
}

//find Y intercept
double findYintercept(double m, double b) {
    return (m*0)+b;
}

Output

Spoiler

Goodbye world!
m: 0.153846
b: -26
X intercept: 169
Y intercept: -26

Process returned 0 (0x0)   execution time : 0.024 s
Press any key to continue.

 

 

Link to post
Share on other sites
Posted · Original PosterOP
19 hours ago, TheOcularMan said:

Take a look at this, compiles and runs, c++


/* The Ocular Man */

#include <iostream>

double findM(double x1, double y1, double x2, double y2);
double findB(double y, double x, double m);
double findXintercept(double m, double b);
double findYintercept(double m, double b);

int main()
{
    std::cout << "Goodbye world!" << std::endl;

    //x-intercept, cross x axis (n,0)
    //y-intercept, cross y axis (0,n)

    //set 1
    int x1 = 3;
    int y1 = -12;
    int x2 = 16;
    int y2 = -10;
    
    //set 2
    /* unused
    int x3 = 8;
    int y3 = -15;

    int x4 = 12;
    int y4 = -1;
    */

    double m = findM(x1, y1, x2, y2);
    std::cout << "m: " << m << std::endl;

    double b = findB(y1, x1, m);
    std::cout << "b: " << b << std::endl;

    double Xi = findXintercept(m, b);
    std::cout << "X intercept: " << Xi << std::endl;

    double Yi = findYintercept(m, b);
    std::cout << "Y intercept: " << Yi << std::endl;

    return 0;
}

//find slope
double findM(double x1, double y1, double x2, double y2) {
    return (y2-y1)/(x2-x1);
}

//find b
double findB(double y, double x, double m) {
    return (y/(m*x));
}

//find X intercept
double findXintercept(double m, double b) {
    return (0-b)/m;
}

//find Y intercept
double findYintercept(double m, double b) {
    return (m*0)+b;
}

Output

  Reveal hidden contents


Goodbye world!
m: 0.153846
b: -26
X intercept: 169
Y intercept: -26

Process returned 0 (0x0)   execution time : 0.024 s
Press any key to continue.

 

 

Hey thanks this is pretty helpful! But I am trying to find the intersection of two line segments...

Link to post
Share on other sites
7 hours ago, stack0s said:

Hey thanks this is pretty helpful! But I am trying to find the intersection of two line segments...

Oh, thought you were looking or intercepts, not intersects. Try this?

/* The Ocular Man */
//Math bits jacked from: http://www.ambrsoft.com/MathCalc/Line/TwoLinesIntersection/TwoLinesIntersection.htm

#include <iostream>
#include <math.h>
#include <stdlib.h>

double findM(double x1, double y1, double x2, double y2);
double findB(double y, double x, double m);

double findXintercept(double m, double b);
double findYintercept(double m, double b);

double findXintersection(double a, double b, double m1, double m2);
double findYintersection(double a, double b, double m1, double m2);

double findTheta(double m1, double m2);

void printLineEqu(double m, double b);

int main()
{
    std::cout << "Goodbye world!" << std::endl;
    
    const double PI = atan(1) * 4; //calculates pi, 3.14159
    std::cout << "PI: " << PI << std::endl;

    //data set 1
    int x1 = 3;
    int y1 = -12;
    int x2 = 16;
    int y2 = -10;

    //data set 2
    int x3 = 8;
    int y3 = -15;
    int x4 = 12;
    int y4 = -1;

    /*
    y = M1x + a
    y = M2x + b
    */

    double m1 = findM(x1, y1, x2, y2);
    double m2 = findM(x3, y3, x4, y4);

    double a = findB(y1, x1, m1);
    double b = findB(y3, x3, m2);

    /* view work so far */
    std::cout << "EQUATION 1: ";
    printLineEqu(m1, a);

    std::cout << "EQUATION 2: ";
    printLineEqu(m2, b);
    /* done */

    //parallel line check
    if((m2 - m1) == 0) {
        std::cout << "both lines are parallel." << std::endl;
        return 0; //exit if out of range
    }

    //calculate theta
    double theta = findTheta(m1, m2);

    if((theta < 0) || (theta > (PI/2))) {
        std::cout << "theta: " << theta << std::endl;
        std::cout << "angle out of range." << std::endl;
        return 0; //exit if out of range
    }

    //find intersections, if all is well
    double Xintersection = findXintersection(a, b, m1, m2);
    std::cout << "X intersection: " << Xintersection << std::endl;

    double Yintersection = findYintersection(a, b, m1, m2);
    std::cout << "Y intersection: " << Yintersection << std::endl;

    /*
    //not used
    double m = findM(x1, y1, x2, y2);
    std::cout << "m: " << m << std::endl;

    double b = findB(y1, x1, m);
    std::cout << "b: " << b << std::endl;

    //x-intercept, cross x axis (n,0)
    //y-intercept, cross y axis (0,n)

    double Xi = findXintercept(m, b);
    std::cout << "X intercept: " << Xi << std::endl;

    double Yi = findYintercept(m, b);
    std::cout << "Y intercept: " << Yi << std::endl;
    */

    return 0;
}

//used for plotting on ti-89 as check, plug and chug
void printLineEqu(double m, double b) {
    if(b < 0) {
        std::cout << "Y = " << m << "*x" << b << std::endl;
    }
    else {
        std::cout << "Y = " << m << "*x+" << b << std::endl;
    }
    return;
}

//find theta
double findTheta(double m1, double m2) {
    double _theta = (m1 - m2) / (1 + (m1 * m2));
    _theta = abs(_theta); //absolute value
    _theta = atan(_theta); //inverse tan
    return _theta;
}

//find the y intersection
double findYintersection(double a, double b, double m1, double m2) {
    return ((a * m2) - (b * m1)) / (m2 - m1);
}

//Fine the x intersection
double findXintersection(double a, double b, double m1, double m2) {
    return (a - b) / (m2 - m1);
}

//find slope
double findM(double x1, double y1, double x2, double y2) {
    return (y2 - y1) / (x2 - x1);
}

//find b
double findB(double y, double x, double m) {
    return y / (m * x);
}

//find X intercept
double findXintercept(double m, double b) {
    return (0 - b) / m;
}

//find Y intercept
double findYintercept(double m, double b) {
    return (m * 0) + b;
}

Output

Spoiler

Goodbye world!
PI: 3.14159
EQUATION 1: Y = 0.153846*x-26
EQUATION 2: Y = 3.5*x-0.535714
X intersection: -7.61002
Y intersection: -27.1708

Process returned 0 (0x0)   execution time : 0.024 s
Press any key to continue.

 

Link to post
Share on other sites
Posted · Original PosterOP
On 9/12/2019 at 5:39 AM, TheOcularMan said:

Oh, thought you were looking or intercepts, not intersects. Try this?


/* The Ocular Man */
//Math bits jacked from: http://www.ambrsoft.com/MathCalc/Line/TwoLinesIntersection/TwoLinesIntersection.htm

#include <iostream>
#include <math.h>
#include <stdlib.h>

double findM(double x1, double y1, double x2, double y2);
double findB(double y, double x, double m);

double findXintercept(double m, double b);
double findYintercept(double m, double b);

double findXintersection(double a, double b, double m1, double m2);
double findYintersection(double a, double b, double m1, double m2);

double findTheta(double m1, double m2);

void printLineEqu(double m, double b);

int main()
{
    std::cout << "Goodbye world!" << std::endl;
    
    const double PI = atan(1) * 4; //calculates pi, 3.14159
    std::cout << "PI: " << PI << std::endl;

    //data set 1
    int x1 = 3;
    int y1 = -12;
    int x2 = 16;
    int y2 = -10;

    //data set 2
    int x3 = 8;
    int y3 = -15;
    int x4 = 12;
    int y4 = -1;

    /*
    y = M1x + a
    y = M2x + b
    */

    double m1 = findM(x1, y1, x2, y2);
    double m2 = findM(x3, y3, x4, y4);

    double a = findB(y1, x1, m1);
    double b = findB(y3, x3, m2);

    /* view work so far */
    std::cout << "EQUATION 1: ";
    printLineEqu(m1, a);

    std::cout << "EQUATION 2: ";
    printLineEqu(m2, b);
    /* done */

    //parallel line check
    if((m2 - m1) == 0) {
        std::cout << "both lines are parallel." << std::endl;
        return 0; //exit if out of range
    }

    //calculate theta
    double theta = findTheta(m1, m2);

    if((theta < 0) || (theta > (PI/2))) {
        std::cout << "theta: " << theta << std::endl;
        std::cout << "angle out of range." << std::endl;
        return 0; //exit if out of range
    }

    //find intersections, if all is well
    double Xintersection = findXintersection(a, b, m1, m2);
    std::cout << "X intersection: " << Xintersection << std::endl;

    double Yintersection = findYintersection(a, b, m1, m2);
    std::cout << "Y intersection: " << Yintersection << std::endl;

    /*
    //not used
    double m = findM(x1, y1, x2, y2);
    std::cout << "m: " << m << std::endl;

    double b = findB(y1, x1, m);
    std::cout << "b: " << b << std::endl;

    //x-intercept, cross x axis (n,0)
    //y-intercept, cross y axis (0,n)

    double Xi = findXintercept(m, b);
    std::cout << "X intercept: " << Xi << std::endl;

    double Yi = findYintercept(m, b);
    std::cout << "Y intercept: " << Yi << std::endl;
    */

    return 0;
}

//used for plotting on ti-89 as check, plug and chug
void printLineEqu(double m, double b) {
    if(b < 0) {
        std::cout << "Y = " << m << "*x" << b << std::endl;
    }
    else {
        std::cout << "Y = " << m << "*x+" << b << std::endl;
    }
    return;
}

//find theta
double findTheta(double m1, double m2) {
    double _theta = (m1 - m2) / (1 + (m1 * m2));
    _theta = abs(_theta); //absolute value
    _theta = atan(_theta); //inverse tan
    return _theta;
}

//find the y intersection
double findYintersection(double a, double b, double m1, double m2) {
    return ((a * m2) - (b * m1)) / (m2 - m1);
}

//Fine the x intersection
double findXintersection(double a, double b, double m1, double m2) {
    return (a - b) / (m2 - m1);
}

//find slope
double findM(double x1, double y1, double x2, double y2) {
    return (y2 - y1) / (x2 - x1);
}

//find b
double findB(double y, double x, double m) {
    return y / (m * x);
}

//find X intercept
double findXintercept(double m, double b) {
    return (0 - b) / m;
}

//find Y intercept
double findYintercept(double m, double b) {
    return (m * 0) + b;
}

Output

  Reveal hidden contents

Goodbye world!
PI: 3.14159
EQUATION 1: Y = 0.153846*x-26
EQUATION 2: Y = 3.5*x-0.535714
X intersection: -7.61002
Y intersection: -27.1708

Process returned 0 (0x0)   execution time : 0.024 s
Press any key to continue.

 

Your calculation for findB is wrong... it should be return y - (m*x) it's not division it's subtraction but other than that this helped me because I was having a hard time with the intersection part which you did very well. I translated it back to a program in actionscript 2.0 that runs once and prints then exits. Here it is...

var test = true;
onEnterFrame = function () {
	if (test) {
		var PI = 3.14195;
		//calculates pi, 3.14159
		//data set 1
		var x1 = 10;
		var y1 = 23;
		var x2 = 65;
		var y2 = -17;
		//data set 2
		var x3 = 49;
		var y3 = 25;
		var x4 = 7;
		var y4 = -12;
		var m1 = findM(x1, y1, x2, y2);
		var m2 = findM(x3, y3, x4, y4);
		var a = findB(y1, x1, m1);
		var b = findB(y3, x3, m2);
		/* view work so far */
		trace("EQUATION 1: ");
		printLineEqu(m1, a);
		trace("EQUATION 2: ");
		printLineEqu(m2, b);
		/* done */
		//parallel line check
		if ((m2-m1) == 0) {
			trace("both lines are parallel.");
			return 0;
			//exit if out of range
		}
		//calculate theta  
		var theta = findTheta(m1, m2);
		if ((theta<0) || (theta>(PI/2))) {
			trace("theta: "+theta);
			trace("angle out of range.");
			return 0;
			//exit if out of range
		}
		//find intersections, if all is well  
		var Xintersection = findXintersection(a, b, m1, m2);
		trace("X intersection: "+Xintersection);
		var Yintersection = findYintersection(a, b, m1, m2);
		trace("Y intersection: "+Yintersection);
		test = false;
	}
};
//used for plotting on ti-89 as check, plug and chug
function printLineEqu(_m:Number, _b:Number) {
	if (_b<0) {
		trace("Y = "+_m+"*x"+_b);
	} else {
		trace("Y = "+_m+"*x+"+_b);
	}
}
//find theta
function findTheta(_m1:Number, _m2:Number):Number {
	_theta = (_m1-_m2)/(1+(_m1*_m2));
	_theta = Math.abs(_theta);
	//absolute value
	_theta = Math.atan(_theta);
	//inverse tan
	return _theta;
}
//find the y intersection
function findYintersection(_a:Number, _b:Number, _m1:Number, _m2:Number):Number {
	return ((_a*_m2)-(_b*_m1))/(_m2-_m1);
}
//Fine the x intersection
function findXintersection(_a:Number, _b:Number, _m1:Number, _m2:Number):Number {
	return (_a-_b)/(_m2-_m1);
}
//find slope
function findM(_x1:Number, _y1:Number, _x2:Number, _y2:Number):Number {
	return (_y2-_y1)/(_x2-_x1);
}
//find b
function findB(_yB:Number, _xB:Number, _mB:Number):Number {
	return (_yB-(_mB*_xB));
}
//find X intercept
function findXintercept(_m:Number, _b:Number):Number {
	return ((0-_b)/_m);
}
//find Y intercept
function findYintercept(_m:Number, _b:Number):Number {
	return ((_m*0)+_b);
}

Thanks alot!

Link to post
Share on other sites
48 minutes ago, stack0s said:

Your calculation for findB is wrong... it should be return y - (m*x) it's not division it's subtraction.

yeah it is.... I don't know how I missed that. Your welcome, glad it helped.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×