0% found this document useful (0 votes)
28 views9 pages

Practica 3

The document defines a Java program that performs the following: 1) Allows the user to input coordinates (x1, y1) and (x2, y2) to define a line segment. 2) Calculates the slope and y-intercept of the line defined by the coordinates. 3) Draws the line segment on a graphical window, displaying the calculated slope, y-intercept, and y-values of points along the line.

Uploaded by

Eduardo Cortes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views9 pages

Practica 3

The document defines a Java program that performs the following: 1) Allows the user to input coordinates (x1, y1) and (x2, y2) to define a line segment. 2) Calculates the slope and y-intercept of the line defined by the coordinates. 3) Draws the line segment on a graphical window, displaying the calculated slope, y-intercept, and y-values of points along the line.

Uploaded by

Eduardo Cortes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

package practica3;

import java.awt.Color;

import java.awt.Frame;

import java.awt.Graphics;

import java.util.Scanner;

public class Practica3 extends Frame {

public Practica3 (){

setTitle("Trazado de línea recta");

this.setSize(800, 800);

this.setVisible(true);

public void paint(Graphics g,int CorX1,int CorX2, int Y)

int x;

g.setColor(Color.RED);

for (x=CorX1;x<=CorX2;x=x+1)

g.drawLine(x,500-Y,x,500-Y);

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);

System.out.println("Ingrese x1");

int CorX1=teclado.nextInt();

System.out.println("Ingrese y1");

int CorY1=teclado.nextInt();

System.out.println("Ingrese x2");

int CorX2=teclado.nextInt();

System.out.println("Ingrese y2");

int CorY2=teclado.nextInt();

Metodos a= new Metodos();

float m = a.Pendiente(CorX1, CorY1, CorX2, CorY2);

float b = a.TerminoIndependiente(CorY1, CorX1,m);

int y = a.CalcularY(m, CorX1, b);

int NumPuntosCandidatos=CorX2-1;

int PuntoSeleccionY;

float f;

for (int x=0;x<NumPuntosCandidatos;x++){


CorX1=CorX1+1;

float f1 = a.Funcion1(CorY1, m, CorX1, b, CorY2);

CorY1=CorY1+1;

float f2 = a.Funcion2(CorY1, m, CorX1, b, CorY2);

if (f1<f2){

PuntoSeleccionY=CorY1-1;

}else{

PuntoSeleccionY=CorY1;

int contf=1;

for (int i=0;i<2;i++){

if (contf==1){

f = f1;

CorY1=CorY1-1;

}else{

f = f2;

CorY1=CorY1+1;

System.out.println("("+CorX1+")"+"("+CorY1+")"+"("+f+")");

contf=contf+1;
}

CorY1=PuntoSeleccionY;

a.ImprimirPantalla(m, b, y);

Practica3 z = new Practica3();

}
package practica3;

import java.awt.Color;

import java.awt.Graphics;

import java.util.Scanner;

public class Metodos {

public float Pendiente(int CorX1,int CorY1,int CorX2,int CorY2){

int dy=CorY2-CorY1;

int dx=CorX2-CorX1;

float m=(float)dy/(float)dx;

return m;

public float TerminoIndependiente(int CorY1,int CorX1, float m){

float b=(float)(CorY1-(m*CorX1));

return b;

public int CalcularY(float m,int CorX1,float b){

int Y=(int)((m*CorX1)+b);

return Y;

public float Funcion1 (int CorY1,float m, int CorX1,float b,int coordenadax){

float f1= CorY1-m*CorX1-b;


if (f1<0){

f1= f1 *-1;

return f1;

return f1;

public float Funcion2(int CorY1,float m, int CorX1, float b,int coordenaday){

float f2= CorY1-m*CorX1-b;

if(f2<0){

f2=f2 *-1;

return f2;

return f2;

public void ImprimirPantalla(float m,float b, float Y){

System.out.println("Pendiente: "+m);

System.out.println("b: "+b);

System.out.println("Y: "+Y);

}
Ejemplo (1,1)(7,4)

Ejemplo(2,30)(20,7)
Ejemplo (2,55)(20,7)
Ejemplo (3,5)(10,30)

You might also like