NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/


import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

/**
*
* @author sajib
*/
public class MidPointLines implements GLEventListener {
/**
* Interface to the GLU library.
*/
private GLU glu;

/**
* Take care of initialization here.
* @param gld
*/
@Override
public void init(GLAutoDrawable gld) {
GL2 gl = gld.getGL().getGL2();
glu = new GLU();

gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glViewport(-250, -150, 250, 150);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
}

/**
* Take care of drawing here.
* @param drawable
*/
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();

gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
/*
* put your code here
*/
//points should be in the same zone
DrawMPL(gl,10,10,60,50);
DrawMPL(gl,10,-10,60,-50);
DrawMPL(gl,-30,-10,-100,-40);
}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
//do nothing
}

public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
//do nothing
}

float dx, dy, d;
private void DrawMPL(GL2 gl, float x1, float y1, float x2, float y2) {
//write your own code
gl.glPointSize(1.0f);
gl.glColor3d(1, 0, 0);

gl.glBegin(GL2.GL_POINTS);


int zonei = findZone(x1,y1,x2,y2);
float x10 = convertX0(x1, y1,zonei);
float y10 = convertY0(x1, y1,zonei);
float x20 = convertX0(x2, y2,zonei);
float y20 = convertY0(x2, y2,zonei);
dx = x20 - x10;
dy = y20 - y10;
d = dy + 2*dx;
float dse = 2*dy + 2*dx;
float ds = 2*dx;

gl.glVertex2f(x1, y1);

while(y10>y20) {
if(d>=0) {
x10 +=1;
y10 -=1;
float xt= convertXi(x10, y10, zonei);
float yt= convertYi(x10, y10, zonei);
gl.glVertex2f(xt, yt);
d = d+dse;
}
else {
y10 -=1;
float xt= convertXi(x10, y10, zonei);
float yt= convertYi(x10, y10, zonei);
gl.glVertex2f(xt, yt);
d = d+ds;
}
}


gl.glEnd();
}

//y=mx+b
private int func(int x, float y){
return (int)(dy*x - y*dx + d*dx);
}


int findZone(float x1, float y1, float x2, float y2) {
float zone = 0;
float dx = x2-x1;
float dy = y2-y1;
if(Math.abs(dx)>=Math.abs(dy)) {
if((dx>=0) && (dy>=0)) {
zone=0;
}
else if ((dx<0) && (dy>=0)) {
zone=3;
}
else if ((dx<0) && (dy<0)) {
zone=4;
}
else {
zone=7;
}
}
else {
if((dx>=0) && (dy>=0)) {
zone=1;
}
else if ((dx<0) && (dy>=0)) {
zone=2;
}
else if ((dx<0) && (dy<0)) {
zone=5;
}
else {
zone=6;
}
}

return (int) zone ;
}

float convertX0(float x, float y, int zone){
float convertedX0=0;
if (zone == 0) return y;
else if (zone == 1) return x;
else if (zone == 2) return -x;
else if (zone == 3) return y;
else if (zone == 4) return -y;
else if (zone == 5) return -x;
else if (zone == 6) return x;
else if (zone == 7) return -y;
return convertedX0;
}

float convertY0(float x, float y, int zone){
float convertedY0=0;
if (zone == 0) return -x;
else if (zone == 1) return -y;
else if (zone == 2) return -y;
else if (zone == 3) return x;
else if (zone == 4) return x;
else if (zone == 5) return y;
else if (zone == 6) return y;
else if (zone == 7) return -x;
return convertedY0;
}
float convertXi(float x, float y, int zone){
float convertedXi=0;
if (zone == 0) return -y;
else if (zone == 1) return x;
else if (zone == 2) return -x;
else if (zone == 3) return y;
else if (zone == 4) return y;
else if (zone == 5) return -x;
else if (zone == 6) return x;
else if (zone == 7) return -y;
return convertedXi;
}

float convertYi(float x, float y, int zone){
float convertedYi=0;
if (zone == 0) return x;
else if (zone == 1) return -y;
else if (zone == 2) return -y;
else if (zone == 3) return x;
else if (zone == 4) return -x;
else if (zone == 5) return y;
else if (zone == 6) return y;
else if (zone == 7) return -x;
return convertedYi;
}


public void dispose(GLAutoDrawable arg0) {
//do nothing
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.