LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 October 15 2015

Charles.S
Member

programming homework help

a. Write an algorithm that determine the coefficients a and b of the equation (y = a * x + b) of a line from two points P and Q.P and Q should be entered by the user.

b. Write an algorithm that determine the point of intersection of two lines having the form y = a * x + b


so I've been stuck on it for hours would appreciate some assistant , Thank you :)

Offline

#2 October 15 2015

m0ei
Member

Re: programming homework help

Both are very simple exercises. Can you solve them mathematically first? or your problem is implementing your solution?

Last edited by m0ei (October 15 2015)

Offline

#3 October 15 2015

Charles.S
Member

Re: programming homework help

programmatically i think i can manage but the math here... it's been a while since i've studied any math :/
appreciate any help

Offline

#4 October 15 2015

Elied
Member

Re: programming homework help

y is a polynomial function of the first degree. You can easily find a and b with 2 points using the relation a= (y(P) - y(Q))/(x(P)-x(Q) then b= y(P)-a*x(P) I'm not 100% sure about the relation regarding "a" but I remember that it was something of the sort if it's something else please correct me. This is basically the Math part, if you need any more help don't hesitate to send me a private message.

Offline

#5 October 15 2015

rolf
Member

Re: programming homework help

Charles.S wrote:

a. Write an algorithm that determine the coefficients a and b of the equation (y = a * x + b) of a line from two points P and Q.P and Q should be entered by the user.

Can you find a and b with only two variables, x an y? I don't think so. What type are P and Q anyway? coordinates (x,y)?

Charles.S wrote:

b. Write an algorithm that determine the point of intersection of two lines having the form y = a * x + b

I could maybe write a big procedural algorithm, but I'm not even sure.

Offline

#6 October 15 2015

m0ei
Member

Re: programming homework help

For the first one, it's well know that

(Y - Yp) / (X - XP) = (Yq - Yp) / (Xq - Xp) 

so if you try to write the above formula in the form of y = ax + b you get the following

y = ( (Yq - Yp) / (Xq - Xp) ) * x + ( ( (Xq - Xp) * Yp + (Yp - Yq) * Xp ) / (Xq - Xp) )

Just wrote it, make sure it's correct. It involves some basic manipulation.

To calculate a and b just map them to their respective in the above equation and you get:

a = (Yq - Yp) / (Xq - Xp)
b = ( (Xq - Xp) * Yp + (Yp - Yq) * Xp ) / (Xq - Xp)
# EDIT: just a small note, that's the generic formula I mentioned, you can easily simplify the b formula and get the following:
b = Yp - a*Xp

So if you have P (1, 2) and Q (2, 4), notice that Q coordinates are double of P, it's expected to get y = 2x where a = 2 and b = 0 which you can calculate with what's mentioned above.


For the second one, you have 2 lines which results in 2 linear equations where you have a and b obviously. If you have 2 equations with a and b given, you can find x and y easily because the interception point is where they both have the same y and x.

so if you set y1 = y2, you get a1 * x + b1 =  a2 * x + b2 and you solve for x where you get something like:

(a1 - a2) * x = b2 - b1
x = (b2 - b1) / (a1 - a2)

you find x then you replace the value of x in any equation you have to calculate the value of y.

Last edited by m0ei (October 15 2015)

Offline

#7 October 15 2015

rolf
Member

Re: programming homework help

For the first question, it's Javascript, it can be run in the browser console (F12)

var X = {};
var Y = {};

['p','q'].forEach(function(p){
	var input = prompt("Enter coordinates to " + p.toUpperCase() + " as 'x,y'");
	var coords = input.split(',').map(function(s){return s.trim();});
	X[p] = coords[0];
	Y[p] = coords[1];
});

// using m0ei's work
var a = (Y.q - Y.p) / (X.q - X.p)
var b = ( (X.q - X.p) * Y.p + (Y.p - Y.q) * X.p ) / (X.q - X.p)

alert("Here are your coefficients, professor:\n a: " + a + ", b: " + b);

Not really an algorithm but meh.

Offline

#8 October 16 2015

xterm
Moderator

Re: programming homework help

How to ask questions

Homework We are not here to solve your homework. You have to prove that you have placed effort to solve your homework prior to asking any question here. We are not smarter or at least we don't claim to be smarter than all the posters available all around the internet, so make sure you do your own research before posting your question. Another thing you need to note which is very important, is that if you don't place some effort to solve your own problems, it means that you're not serious about your field and if you're not serious about your field, we will bash you in a very rude manner. Software Development and Systems Administration and the lot are already polluted with people that have just cheated their way into their jobs. Jobs, that are better suited for people that are quite capable of providing great benefits. Understand that we're not stupid, we can distinguish homework from anything else. In summary, we will NOT carry you through your career.

Offline

Board footer