/** * Desenha a reta de acordo com as coordenadas dos pontos selecionados na tela e de acordo com o algoritmo escolhido */ private void DesenharReta(MouseEventArgs e) { if (quantidadeClick == 1) { this.reta.SetX1(e.X); this.reta.SetY1(e.Y); txtX.Text = Convert.ToString(this.reta.GetX1()); txtY.Text = Convert.ToString(this.reta.GetY1()); this.Colore(this.reta.GetX1(), this.reta.GetY1()); } else if (quantidadeClick == 2) { this.reta.SetX2(e.X); this.reta.SetY2(e.Y); this.reta.SetDx(this.reta.GetX2() - this.reta.GetX1()); this.reta.SetDy(this.reta.GetY2() - this.reta.GetY1()); this.reta.SetCoeficienteAngular(this.reta.GetDy() / this.reta.GetDx()); Reta retaAux = new Reta(this.reta.GetX1(), this.reta.GetY1(), this.reta.GetX2(), this.reta.GetY2()); this.listaRetas.Add(retaAux); txtX.Text = Convert.ToString(this.reta.GetX2()); txtY.Text = Convert.ToString(this.reta.GetY2()); this.Colore(this.reta.GetX2(), this.reta.GetY2()); this.RecortCohen(this.reta.GetX1(), this.reta.GetY1(), this.reta.GetX2(), this.reta.GetY2()); this.quantidadeClick = 0; } }
/** * Função de recorte de retas */ void LiangBarsky(double x1, double y1, double x2, double y2) { double u1 = 0; double u2 = 1; double dx = x2 - x1; double dy = y2 - y1; if (cliptest(-dx, x1, ref u1, ref u2)) { if (cliptest(dx, imagem.Size.Width - 1 - x1, ref u1, ref u2)) { if (cliptest(-dy, y1, ref u1, ref u2)) { if (cliptest(dy, imagem.Size.Height - 1 - y1, ref u1, ref u2)) { if (u2 < 1) { x2 = (int)(x1 + dx * u2); y2 = (int)(y1 + dy * u2); } if (u1 > 0) { x1 = (int)(x1 + dx * u1); y1 = (int)(y1 + dy * u1); } this.reta = new Reta(x1, y1, x2, y2); if (this.algoritmoEscolhido == Enum.Retas.DDA) { this.AlgoritmoDDA(); } else if (this.algoritmoEscolhido == Enum.Retas.BRESENHAM) { this.AlgoritmoRetaBresenham(); } else if (this.algoritmoEscolhido == Enum.TRANSFORMACOES.TRANSLADAR || this.algoritmoEscolhido == Enum.TRANSFORMACOES.ROTACIONAR || this.algoritmoEscolhido == Enum.TRANSFORMACOES.ESCALA_AUMENTAR || this.algoritmoEscolhido == Enum.TRANSFORMACOES.ESCALA_DIMINUIR || this.algoritmoEscolhido == Enum.TRANSFORMACOES.REFLETIRX || this.algoritmoEscolhido == Enum.TRANSFORMACOES.REFLETIRY || this.algoritmoEscolhido == Enum.TRANSFORMACOES.REFLETIRXY) { this.AlgoritmoRetaBresenham(); } } } } } }
public tela() { InitializeComponent(); areaDesenho = new Bitmap(imagem.Size.Width, imagem.Size.Height); corPreenche = Color.Black; quantidadeClick = 0; algoritmoEscolhido = -1; recorteEscolhido = Enum.RECORTE.COHEN; desenharReta = false; desenharCircunferencia = false; transformar = false; reta = new Reta(); circunferencia = new Circunferencia(); listaRetas = new List <Reta>(); listaCircunferencias = new List <Circunferencia>(); this.x1 = 0.0; this.x2 = 0.0; this.y1 = 0.0; this.y2 = 0.0; this.VerificarComponentes(); }
/** * Função de recorte de retas */ public void RecortCohen(double x1, double y1, double x2, double y2) { int code1 = getPointCode(x1, y1); int code2 = getPointCode(x2, y2); bool accept = false; while (true) { if ((code1 == 0) && (code2 == 0)) { accept = true; break; } else if ((code1 & code2) != 0) { break; } else { int code_out; double x = 0, y = 0; if (code1 != 0) { code_out = code1; } else { code_out = code2; } if ((code_out & 8) != 0) { x = x1 + (x2 - x1) * (imagem.Size.Height - y1) / (y2 - y1); y = imagem.Size.Height - 1; } else if ((code_out & 4) != 0) { x = x1 + (x2 - x1) * (0 - y1) / (y2 - y1); y = 0; } else if ((code_out & 2) != 0) { y = y1 + (y2 - y1) * (imagem.Size.Width - x1) / (x2 - x1); x = imagem.Size.Width - 1; } else if ((code_out & 1) != 0) { y = y1 + (y2 - y1) * (0 - x1) / (x2 - x1); x = 0; } if (code_out == code1) { x1 = (int)x; y1 = (int)y; code1 = getPointCode(x1, y1); } else { x2 = (int)x; y2 = (int)y; code2 = getPointCode(x2, y2); } } } if (accept) { this.reta = new Reta(x1, y1, x2, y2); if (this.algoritmoEscolhido == Enum.Retas.DDA) { this.AlgoritmoDDA(); } else if (this.algoritmoEscolhido == Enum.Retas.BRESENHAM) { this.AlgoritmoRetaBresenham(); } else if (this.algoritmoEscolhido == Enum.TRANSFORMACOES.TRANSLADAR || this.algoritmoEscolhido == Enum.TRANSFORMACOES.ROTACIONAR || this.algoritmoEscolhido == Enum.TRANSFORMACOES.ESCALA_AUMENTAR || this.algoritmoEscolhido == Enum.TRANSFORMACOES.ESCALA_DIMINUIR || this.algoritmoEscolhido == Enum.TRANSFORMACOES.REFLETIRX || this.algoritmoEscolhido == Enum.TRANSFORMACOES.REFLETIRY || this.algoritmoEscolhido == Enum.TRANSFORMACOES.REFLETIRXY) { this.AlgoritmoRetaBresenham(); } } }