Linear Equation
A linear equation has the form ax + by = c, where x and y are variables and a, b, and c are coefficients. Linear equations represent straight lines and have a wide range of applications in math, physics, and computer graphics.
Java example solving a 2x2 system:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 void solve2Eq(double a1, double b1, double c1,
double a2, double b2, double c2) {
double det = a1*b2  a2*b1;
double x = (b2*c1  b1*c2) / det;
double y = (a1*c2  a2*c1) / det;
System.out.println(x + "," + y);
}
// Solve example system:
// 2x + 3y = 5
// 3x  2y = 7
solve2Eq(2, 3, 5, 3, 2, 7);

C++ example finding intersection:
1
2
3
4
5
6
7
8
9
10
 // Lines: p1 + r1*t, p2 + r2*t
// Solves for t at intersection
double solve(Vector2 p1, Vector2 r1, Vector2 p2, Vector2 r2) {
Vector2 r3 = p2  p1;
double det = crossProduct(r1, r2);
double t = crossProduct(r3, r2) / det;
return t;
}

Python example solving ax + b = 0:
1
2
3
4
5
6
7
8
9
 def solveLinear(a, b):
if a == 0:
if b == 0:
return "Infinite solutions"
else:
return "No solution"
x = b/a
return x

Linear equations are fundamental building blocks in math and physics engines.
A linear equation is an algebraic equation in which each term is a constant or the product of a constant and a single variable. The equation takes the form (ax + b = 0), where (a) and (b) are constants and (x) is the variable. Linear equations are fundamental to algebra and are the building blocks for more complex equations and systems. They represent straight lines when graphed in two dimensions.
In the context of computer science, solving a linear equation often involves finding the value of (x) that satisfies the equation. Different methods can be used, including substitution, elimination, or using matrix methods for systems of linear equations.
Example Code
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 public class LinearEquation {
public static void main(String[] args) {
double a = 3, b = 9;
double x = solveLinearEquation(a, b);
System.out.println("Solution: x = " + x);
}
public static double solveLinearEquation(double a, double b) {
if (a == 0) {
throw new IllegalArgumentException("a cannot be zero");
}
return b / a;
}
}

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 #include <iostream>
#include <stdexcept>
using namespace std;
double solveLinearEquation(double a, double b) {
if (a == 0) {
throw invalid_argument("a cannot be zero");
}
return b / a;
}
int main() {
double a = 3, b = 9;
double x = solveLinearEquation(a, b);
cout << "Solution: x = " << x << endl;
return 0;
}

Python
1
2
3
4
5
6
7
8
9
 def solve_linear_equation(a, b):
if a == 0:
raise ValueError("a cannot be zero")
return b / a
a = 3
b = 9
x = solve_linear_equation(a, b)
print(f"Solution: x = {x}")

In these examples, the function solveLinearEquation
takes (a) and (b) as input parameters and returns the value of (x) that satisfies the equation (ax + b = 0). Note that we check for (a) being zero, as that would make the equation undefined.