package cg.skn.pl; /*
import android.app.Activity;
import android.os.Bundle;
import android.graphics.Color; Android
import android.content.Context; Libraries
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View; */
public class LineActivity extends Activity //classLineActivity
{
DrawLine drawLine; //DrawLine()
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
drawLine = new DrawLine(this);
drawLine.setBackgroundColor(Color.CYAN);
setContentView(drawLine);
}
class DrawLine extends View
{
Paint paint = new Paint();
public DrawLine(Context context)
{
//To allow the Android Developer Tools to interact with your view.
//Must provide a constructor that //takes a Context as parameter.
//This constructor allows the layout editor to create and edit an instance of your view.
super(context);
}@Override
public void onDraw(Canvas canvas)
{ // canvas is passed to a onDraw() method
int[] x1 = new int[20];
int[] y1 = new int[20];
int[] inter = new int [20];
int x, xmin,ymin,xmax,ymax,i, c;
x1[0]= 10;
y1[0] = 10;
x1[1]= 100;
y1[1] = 100;
x1[2]= 100;
y1[2] = 10;
x1[3]= 10;
y1[3] = 100;
x1[4]= x1[0];
y1[4] = y1[0];
xmin=xmax=x1[0];
ymin=ymax=y1[0];
for( i=0;i<4;i++)
{
if(xmin>x1[i])
xmin=x1[i];
if(xmax<x1[i])
xmax=x1[i];
if(ymin>y1[i])
ymin=y1[i];
if(ymax<y1[i])
ymax=y1[i];
}double s ;
s=ymin+0.5;
while(s<=ymax)
{
int ex1,ex2,ey1,ey2,temp;
float z = 0;
c=0;
for( i=0;i<4;i++)
{
ex1=x1[i];
ey1=y1[i];
ex2=x1[i+1];
ey2=y1[i+1];
if(ey2<ey1)
{
temp=ex1;
ex1=ex2;
ex2=temp;
temp=ey1;
ey1=ey2;
ey2=temp;
}
z = (float) s;
if(z<=ey2&&z>=ey1)
{
if((ey1-ey2)==0)
x=ex1;
else
{
x=(int)
(((ex2-ex1)*(z-ey1))/(ey2-ey1));
x=x+ex1;
}
if(x<=xmax && x>=xmin)
inter[c++]=x;
}
}
int temp1,j,w=1;
for( i=0;i<c;i++)
{
for(j=i+1;j<c;j++)
{
if(inter[i]>inter[j])
{
temp=inter[i]; inter[i]=inter[j];
inter[j]=temp;
}
}
}
if(w==1)
{
for(i=0;i<4;i++)
canvas.drawLine(x1[i],y1[i],x1[i+1],
y1[i+1],paint);
for(i=0; i<c;i+=2)
canvas.drawLine(inter[i],z,
inter[i+1],z,paint);
}
s++;
}
}
}
}
import android.app.Activity;
import android.os.Bundle;
import android.graphics.Color; Android
import android.content.Context; Libraries
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View; */
public class LineActivity extends Activity //classLineActivity
{
DrawLine drawLine; //DrawLine()
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
drawLine = new DrawLine(this);
drawLine.setBackgroundColor(Color.CYAN);
setContentView(drawLine);
}
class DrawLine extends View
{
Paint paint = new Paint();
public DrawLine(Context context)
{
//To allow the Android Developer Tools to interact with your view.
//Must provide a constructor that //takes a Context as parameter.
//This constructor allows the layout editor to create and edit an instance of your view.
super(context);
}@Override
public void onDraw(Canvas canvas)
{ // canvas is passed to a onDraw() method
int[] x1 = new int[20];
int[] y1 = new int[20];
int[] inter = new int [20];
int x, xmin,ymin,xmax,ymax,i, c;
x1[0]= 10;
y1[0] = 10;
x1[1]= 100;
y1[1] = 100;
x1[2]= 100;
y1[2] = 10;
x1[3]= 10;
y1[3] = 100;
x1[4]= x1[0];
y1[4] = y1[0];
xmin=xmax=x1[0];
ymin=ymax=y1[0];
for( i=0;i<4;i++)
{
if(xmin>x1[i])
xmin=x1[i];
if(xmax<x1[i])
xmax=x1[i];
if(ymin>y1[i])
ymin=y1[i];
if(ymax<y1[i])
ymax=y1[i];
}double s ;
s=ymin+0.5;
while(s<=ymax)
{
int ex1,ex2,ey1,ey2,temp;
float z = 0;
c=0;
for( i=0;i<4;i++)
{
ex1=x1[i];
ey1=y1[i];
ex2=x1[i+1];
ey2=y1[i+1];
if(ey2<ey1)
{
temp=ex1;
ex1=ex2;
ex2=temp;
temp=ey1;
ey1=ey2;
ey2=temp;
}
z = (float) s;
if(z<=ey2&&z>=ey1)
{
if((ey1-ey2)==0)
x=ex1;
else
{
x=(int)
(((ex2-ex1)*(z-ey1))/(ey2-ey1));
x=x+ex1;
}
if(x<=xmax && x>=xmin)
inter[c++]=x;
}
}
int temp1,j,w=1;
for( i=0;i<c;i++)
{
for(j=i+1;j<c;j++)
{
if(inter[i]>inter[j])
{
temp=inter[i]; inter[i]=inter[j];
inter[j]=temp;
}
}
}
if(w==1)
{
for(i=0;i<4;i++)
canvas.drawLine(x1[i],y1[i],x1[i+1],
y1[i+1],paint);
for(i=0; i<c;i+=2)
canvas.drawLine(inter[i],z,
inter[i+1],z,paint);
}
s++;
}
}
}
}
Output
No comments:
Post a Comment