mfc - Dragging points to change the shapes -
i have managed connect 4 points mouse click made using following code.(i using mfc).
void cchildview::onlbuttondown(uint nflags,cpoint point) { if(m_lastpoint.x!=-1 && m_ipointamount<6) { cdc* pdc= getdc(); pdc->moveto(m_lastpoint.x,m_lastpoint.y); pdc->lineto(point.x,point.y); } m_lastpoint=point; m_ipointamount++; }
the variables initialized in constructor follows.
m_lastpoint.x=-1; m_lastpoint.y=-1; m_ipointamount=1;
i want following now.
1.whenever mouse click made on 1 of points , dragged, point should relocate new position.(so shape changed).
2.this should applicable 4 points.
pls guide me on how achieve this.
how works
start clicking on window until reach amount sepcified in m_ipolygonmaximumsides. when reach amount of points specified polygon closed, able select point. click near point select it, drag keep going until click again. reset polygon have close window.
code
in cpp file of cchildview class add following include, because used calculate square root
#include <math.h>
in cchildview class add following methods , variables
clist<cpoint> m_pointlist; cpoint m_selectedpoint; int m_ipolygonmaximumsides; afx_msg void onmousemove(uint nflags, cpoint point); void drawpolygonfromlist();
in message map of cchildview class add these lines
on_wm_lbuttondown() on_wm_mousemove()
in cchildview constructor initialize m_ipolygonmaximumsides amount want (4) , m_selectedpoint (-1,-1) like
m_ipolygonmaximumsides = 4; m_selectedpoint = cpoint(-1,-1);
then added cchildview cpp file, these moethods used
void cchildview::onlbuttondown(uint nflags, cpoint point) { //check if reached maximum point number if (m_pointlist.getsize() == m_ipolygonmaximumsides) { //check if point selected, , in drag mode if (m_selectedpoint.x !=-1) { //if point selected means want stop dragging, //so set x -1 m_selectedpoint.x =-1; } else { //if didn't have point selected have check if point clicked 1 of our points //so use search out point (position pos = m_pointlist.getheadposition();pos != null;m_pointlist.getnext(pos)) { cpoint currentpoint = m_pointlist.getat(pos); //this pythagorean theorem find distance between 2 points , b // distance = squareroot( (a.x-b.x)^2+(a.y-b.y)^2) int distancebetweenpoints = floor(sqrt(pow(double(currentpoint.x-point.x),2)+pow(double(currentpoint.y-point.y),2))); //if distance less 10 pixels, accept click on our points //this tollerance, can reduce or increment //the smaller tollerance nearer have click able select point if (distancebetweenpoints <= 10) { //if tollerance met set point our selected point m_selectedpoint = currentpoint; //interrupt iteration, because don't need further break; } } } } //if didn't reach maximum point amount means still have keep going on getting points else if (m_pointlist.getsize() > 0) { cdc* pdc= getdc(); cpoint lastpoint = m_pointlist.gettail(); //draw line previous (last) point new 1 pdc->moveto(lastpoint.x,lastpoint.y); pdc->lineto(point.x,point.y); //if going reach maximum amount of points have close polygon if (m_pointlist.getsize()==m_ipolygonmaximumsides-1) { cpoint firstpoint = m_pointlist.gethead(); //draw line current point first point pdc->moveto(point.x,point.y); pdc->lineto(firstpoint.x,firstpoint.y); } } //add point list after have done everything, if didn't reachthe maximum amount if (m_pointlist.getsize() < m_ipolygonmaximumsides) m_pointlist.addtail(point); } void cchildview::onmousemove(uint nflags, cpoint point) { //check if have maximum number of points if (m_pointlist.getsize()==m_ipolygonmaximumsides) { //check if (m_selectedpoint.x != -1) { //check if find point position posfound = m_pointlist.find(m_selectedpoint); if (posfound != null) { //update selected point new 1 m_selectedpoint=point; //now update list m_pointlist.setat(posfound,point); //draw polygon drawpolygonfromlist(); } } } } void cchildview::drawpolygonfromlist() { //this checked again because might want use function in place if (m_pointlist.getsize()==m_ipolygonmaximumsides) { //use clear window redrawwindow(); //this used draw cdc* pdc= getdc(); position pos = m_pointlist.getheadposition(); //load first , second point cpoint pointbefore = m_pointlist.getnext(pos); cpoint currentpoint = m_pointlist.getnext(pos); //draw line connecting first , second point pdc->moveto(pointbefore.x,pointbefore.y); pdc->lineto(currentpoint.x,currentpoint.y); //draw intermediary points while (pos != null) { pointbefore = currentpoint; currentpoint = m_pointlist.getnext(pos); pdc->moveto(pointbefore.x,pointbefore.y); pdc->lineto(currentpoint.x,currentpoint.y); } //now close poligon pointbefore = currentpoint; currentpoint = m_pointlist.gethead(); pdc->moveto(pointbefore.x,pointbefore.y); pdc->lineto(currentpoint.x,currentpoint.y); } }
Comments
Post a Comment