📄 connect.cc
字号:
//**************************************************************//* filename: connect.cc *//* *//**************************************************************//* programmed by: Thomas Wagner *//* last change: 22-05-95 *//**************************************************************#include <stdio.h>#include <stdlib.h>#include <values.h>#include <X11/Xlib.h>#include "daten.h"#include "connect.h"extern blackpix, buttonpix;void CalculatePoints (XPoint Points[]){ double length; double xdiff, ydiff; length = sqrt ((Points[2].x - Points[0].x) * (Points[2].x - Points[0].x) + (Points[2].y - Points[0].y) * (Points[2].y - Points[0].y)); if (length == 0) length = 1.0; xdiff = (Points[2].x - Points[0].x) / length; ydiff = (Points[2].y - Points[0].y) / length; Points[1].x = Points[0].x - (int) (ARROWWIDTH * ydiff); Points[1].y = Points[0].y + (int) (ARROWWIDTH * xdiff); Points[3].x = Points[0].x + (int) (ARROWWIDTH * ydiff); Points[3].y = Points[0].y - (int) (ARROWWIDTH * xdiff);}Connection::Connection (Display * initdisplay, GC initgc, MainWindow * parentwindow, short startx, short starty, short endx, short endy){ status = CONNECTSTATUS_VISIBLE; Start = End = 0; Points[0].x = startx; Points[0].y = starty; Points[2].x = endx; Points[2].y = endy; CalculatePoints (Points); gc = initgc; display = initdisplay; mainwindow = parentwindow; *(mainwindow->GetConnectlist ()) += this;};Connection::~Connection (){ *(mainwindow->GetConnectlist ()) -= this;}MoveableIcon *Connection::GetStartIcon (){ return Start;}MoveableIcon *Connection::GetEndIcon (){ return End;}void Connection::SendActiontoStart (int actionnumber, int value){ Start->Action (actionnumber, value);}void Connection::SendActiontoEnd (int actionnumber, int value){ End->Action (actionnumber, value);}double Connection::CalculateDistance (int x, int y, char *pointtyp){// first: test if (x,y) is in the area between Points[0] and Points[2] // diff: vector between Points[0] and Points[2] double diff[2]; double d; diff[0] = (Points[2].x - Points[0].x); diff[1] = (Points[2].y - Points[0].y);// calculate cos*const (const:vectors not normified) d = (x - Points[0].x) * diff[0] + (y - Points[0].y) * diff[1]; if (d < 0.0) // distance is distance between (x,y) and Points[0] { *pointtyp = STARTPOINT; return sqrt ((Points[0].x - x) * (Points[0].x - x) + (Points[0].y - y) * (Points[0].y - y)); }// calculate cos*const (const:vectors not normified) d = (Points[2].x - x) * diff[0] + (Points[2].y - y) * diff[1]; if (d < 0.0) // distance is distance between (x,y) and Points[2] { *pointtyp = ENDPOINT; return sqrt ((Points[2].x - x) * (Points[2].x - x) + (Points[2].y - y) * (Points[2].y - y)); }// calculating distance of (x,y) from line g // hessische normalform g:n*p-d=0 // distance of p1 from g is distance=n*p1-d // (d=n*p ,p is point on g) // n: normalvector // p1:(x,y) // d: d double distance; double length = sqrt ((Points[2].x - Points[0].x) * (Points[2].x - Points[0].x) + (Points[2].y - Points[0].y) * (Points[2].y - Points[0].y)); double normalvector[2]; if (length != 0.0) { normalvector[0] = -diff[1] / length; normalvector[1] = diff[0] / length; } else { normalvector[0] = 1.0; normalvector[1] = 0.0; } d = Points[0].x * normalvector[0] + Points[0].y * normalvector[1]; distance = x * normalvector[0] + y * normalvector[1] - d;// calculate if start- or endpoint if (((Points[2].x - x) * (Points[2].x - x) + (Points[2].y - y) * (Points[2].y - y)) < ((Points[0].x - x) * (Points[0].x - x) + (Points[0].y - y) * (Points[0].y - y))) *pointtyp = ENDPOINT; else *pointtyp = STARTPOINT; if (distance > 0) return distance; else return -distance;}void Connection::SetStart (short x, short y){ Points[0].x = x; Points[0].y = y; CalculatePoints (Points);}void Connection::SetEnd (short x, short y){ Points[2].x = x; Points[2].y = y; CalculatePoints (Points);}void Connection::MoveStart (short x, short y){ SetStart (GetStartx () + x, GetStarty () + y); CalculatePoints (Points);}void Connection::MoveEnd (short x, short y){ SetEnd (GetEndx () + x, GetEndy () + y); CalculatePoints (Points);}void Connection::ErasetempArrow (){ XSetForeground (display, gc, blackpix); XSetFunction (display, gc, GXxor); XDrawLines (display, mainwindow->GetWindow (), gc, Points + 1, ARROWPOINTNUMBER, CoordModeOrigin); XSetFunction (display, gc, GXcopy);}void Connection::DrawtempArrow (){ XSetFunction (display, gc, GXxor); CalculatePoints (Points); XSetForeground (display, gc, blackpix); XDrawLines (display, mainwindow->GetWindow (), gc, Points + 1, ARROWPOINTNUMBER, CoordModeOrigin); XSetFunction (display, gc, GXcopy);}void Connection::DrawtempArrow (short x, short y){ XSetFunction (display, gc, GXxor); Points[2].x = x; Points[2].y = y; CalculatePoints (Points); XSetForeground (display, gc, blackpix); XDrawLines (display, mainwindow->GetWindow (), gc, Points + 1, ARROWPOINTNUMBER, CoordModeOrigin); XSetFunction (display, gc, GXcopy);}void Connection::DrawArrow (){ XSetForeground (display, gc, blackpix); XFillPolygon (display, mainwindow->GetWindow (), gc, Points + 1, ARROWPOINTNUMBER, Convex, CoordModeOrigin);}void Connection::EraseArrow (){ XSetForeground (display, gc, buttonpix); XFillPolygon (display, mainwindow->GetWindow (), gc, Points + 1, ARROWPOINTNUMBER, Convex, CoordModeOrigin);}Connectlist::Connectlist (){ connection = NULL; Next = NULL; Previous = NULL;}char Connectlist::ConnectionExists (MoveableIcon * starticon, MoveableIcon * endicon){ Connectlist *temp = this; while (temp != NULL) { if ((temp->connection->Start == starticon) && (temp->connection->End == endicon)) return TRUE; temp = temp->Next; } return FALSE;}void Connectlist::operator+= (Connection * newconnection){ if (connection == NULL) { connection = newconnection; first = this; } else { Connectlist *temp = this; while (temp->Next != NULL) temp = temp->Next; temp->Next = new Connectlist (); temp->Next->Previous = temp; temp = temp->Next; temp->first = this; temp->connection = newconnection; }}void Connectlist::operator-= (Connection * delconnection){ Connectlist *temp = this, *temp2;// first find connection while ((temp != NULL) && (temp->connection != delconnection)) temp = temp->Next; if (temp != NULL) //connection found { if (temp->Next == NULL) // connection in last element { temp->connection = NULL; if (temp->Previous != NULL) { temp->Previous->Next = NULL; delete temp; } } else { temp2 = temp->Next; temp->connection = temp->Next->connection; temp->Next = temp->Next->Next; if (temp2 != NULL) { temp2->Next = NULL; temp2->connection = NULL; delete temp2; } } }}void Connectlist::SendActiontoallEnds (int actionnumber, int value){ Connectlist *temp = this; Connection *connection = temp->GetConnection (); while (connection != NULL) { connection->SendActiontoEnd (actionnumber, value); temp = temp->GetNext (); if (temp != NULL) connection = temp->GetConnection (); else connection = NULL; }}void Connectlist::DrawAllArrow (){ Connectlist *temp = this; Connection *connection = temp->GetConnection (); while (connection != NULL) { if (connection->status & CONNECTSTATUS_VISIBLE) connection->DrawArrow (); temp = temp->GetNext (); if (temp != NULL) connection = temp->GetConnection (); else connection = NULL; }}Connection *Connectlist::FindnearestConnection (int x, int y, char *pointtyp){ char temppointtyp; double mindistance = MAXFLOAT; Connection *nearest = 0; Connectlist *temp = this; Connection *connection = temp->GetConnection (); while (connection != NULL) { double distance = connection->CalculateDistance (x, y, &temppointtyp); if (distance < mindistance) { mindistance = distance; nearest = connection; *pointtyp = temppointtyp; } temp = temp->GetNext (); if (temp != NULL) connection = temp->GetConnection (); else connection = NULL; } return nearest;}Connectlist::~Connectlist (){ if (connection != NULL) delete connection; if (Next != NULL) delete Next;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -